|
1 | 1 | ---
|
2 |
| -title : $(document).ready() |
| 2 | +title : $( document ).ready() |
3 | 3 | level: beginner
|
4 | 4 | ---
|
5 |
| -You cannot safely manipulate a page until the document is “ready.” |
6 |
| -jQuery detects this state of readiness for you; code included inside |
7 |
| -`$(document).ready()` will only run once the page DOM (Document Object Model) is ready for JavaScript code to execute |
8 |
| -whereas `$(window).load(function () {})` will run once the entire page (images or iframe) not just the DOM is ready. |
9 |
| - |
10 |
| -``` js |
11 |
| -// A $(document).ready() block |
12 |
| -$(document).ready(function() { |
13 |
| - console.log('ready!'); |
| 5 | +You cannot safely manipulate a page until the document is “ready.” |
| 6 | +jQuery detects this state of readiness for you; code included inside |
| 7 | +`$( document ).ready()` will only run once the page DOM (Document Object Model) is ready for JavaScript code to execute |
| 8 | +whereas `$( window ).load(function () {})` will run once the entire page (images or iframe) not just the DOM is ready. |
| 9 | + |
| 10 | +``` |
| 11 | +// A $( document ).ready() block |
| 12 | +$( document ).ready(function() { |
| 13 | +
|
| 14 | + console.log("ready!"); |
| 15 | +
|
14 | 16 | });
|
15 | 17 | ```
|
16 | 18 |
|
17 |
| -There is a shorthand for `$(document).ready()` that you will sometimes see; however, |
18 |
| -I recommend against using it if you are writing code that people who aren't experienced |
| 19 | +There is a shorthand for `$( document ).ready()` that you will sometimes see; however, |
| 20 | +I recommend against using it if you are writing code that people who aren't experienced |
19 | 21 | with jQuery may see.
|
20 | 22 |
|
21 |
| -``` js |
22 |
| -// Shorthand for $(document).ready() |
| 23 | +``` |
| 24 | +// Shorthand for $( document ).ready() |
23 | 25 | $(function() {
|
24 |
| - console.log('ready!'); |
| 26 | +
|
| 27 | + console.log("ready!"); |
| 28 | +
|
25 | 29 | });
|
26 | 30 | ```
|
27 | 31 |
|
28 | 32 | You can also pass a named function to `$(document).ready()` instead of passing an anonymous function.
|
29 | 33 |
|
30 |
| -``` js |
| 34 | +``` |
31 | 35 | // Passing a named function instead of an anonymous function
|
32 | 36 |
|
33 | 37 | function readyFn( jQuery ) {
|
| 38 | +
|
34 | 39 | // code to run when the document is ready
|
| 40 | +
|
35 | 41 | }
|
36 | 42 |
|
37 |
| -$(document).ready(readyFn); |
| 43 | +$( document ).ready( readyFn ); |
38 | 44 |
|
39 |
| -// OR |
| 45 | +// OR |
40 | 46 |
|
41 |
| -$(window).load(readyFn); |
| 47 | +$( window ).load( readyFn ); |
42 | 48 | ```
|
43 | 49 |
|
44 |
| -Let's take a look at how both the events act. The below example tries to load some website url in an iframe and checks for both the events. |
| 50 | +Let's take a look at how both the events act. The below example tries to load some website url in an iframe and checks for both the events. |
45 | 51 | ``` html
|
46 | 52 | <html>
|
47 | 53 | <head>
|
48 | 54 | <script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
|
49 |
| - <script> |
50 |
| - $(document).ready(function () { |
51 |
| - console.log('document loaded'); |
52 |
| - }); |
53 |
| - |
54 |
| - $(window).load(function () { |
55 |
| - console.log('window loaded'); |
56 |
| - }); |
57 |
| - </script> |
| 55 | + <script> |
| 56 | +
|
| 57 | + $( document ).ready(function () { |
| 58 | +
|
| 59 | + console.log("document loaded"); |
| 60 | +
|
| 61 | + }); |
| 62 | +
|
| 63 | + $( window ).load(function () { |
| 64 | +
|
| 65 | + console.log("window loaded"); |
| 66 | +
|
| 67 | + }); |
| 68 | + </script> |
58 | 69 | </head>
|
59 | 70 | <body>
|
60 |
| - <iframe src='http://techcrunch.com'></iframe> |
| 71 | + <iframe src="http://techcrunch.com"></iframe> |
61 | 72 | </body>
|
62 | 73 | </html>
|
63 | 74 | ```
|
0 commit comments