Skip to content

Commit 8999811

Browse files
committed
Add document-ready for jq-basics
1 parent 3ce4f33 commit 8999811

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
chapter : "jqfundamentals"
3+
section : "1"
4+
title : "$(document).ready()"
5+
---
6+
## $(document).ready()
7+
8+
You cannot safely manipulate a page until the document is “ready.” jQuery detects this state of readiness for you; code included inside `$(document).ready()` will only run once the page is ready for JavaScript code to execute.
9+
10+
<div class="example" markdown="1">
11+
A $(document).ready() block
12+
13+
$(document).ready(function() {
14+
console.log('ready!');
15+
});
16+
</div>
17+
18+
There is a shorthand for `$(document).ready()` that you will sometimes see; however, I recommend against using it if you are writing code that people who aren't experienced with jQuery may see.
19+
20+
<div class="example" markdown="1">
21+
Shorthand for $(document).ready()
22+
23+
$(function() {
24+
console.log('ready!');
25+
});
26+
</div>
27+
28+
You can also pass a named function to `$(document).ready()` instead of passing an anonymous function.
29+
30+
<div class="example" markdown="1">
31+
Passing a named function instead of an anonymous function
32+
function readyFn() {
33+
// code to run when the document is ready
34+
}
35+
</div>
36+
37+
`$(document).ready(readyFn);`

0 commit comments

Comments
 (0)