JavaScript has a function of window.onload = function()
,
which is executed after the page is loaded.
jQuery also has similar function of $(document).ready(runction())
,
but they are a little different.
- window.onload
- $(document).ready
- Implement $(document).ready like behavior w/o jQuery
- Multiple registration at onload
- Execution order
window.onload
window.onload
method is executed when onload
even occurs.
This means when not only DOM tree is built, but also
all data, such images, are loaded.
To give assign a function, it is like:
1 2 3 |
|
Therefore, if you have another liens like below, it will overwrite the previous one.
1 2 3 |
|
In this case, only onload 2
is shown.
There are another method of document.body.onload
,
which in principle works in the same way as window.onload
,
but it behaves a little different in different browser,
so that window.onload
is recommended rather than document.body.onload
.
$(document).ready
This is usually used when jQuery is used.
This is executed when DOM tree is built. It doesn’t wait loading such images.
Therefore, it is executed earlier than window.onload
.
The code is like:
1 2 3 |
|
This works to put a function, so multiple inputs can be accepted.
You can omit some parts and can write like:
1 2 3 |
|
If you have above both, a console shows ready 1
and ready 2
.
If $
has a name collision problem, it should be like
1 2 3 |
|
And following style with on
works similar,
but a little different (see below).
1 2 3 |
|
Implement $(document).ready like behavior w/o jQuery
Main differences between window.onload
and $(document).ready
are
the timing when it is executed,
whether it can have multiple components or not.
For the timing, you can do most of the things when DOM tree is built, and might want to start w/o waiting images.
In addition, it is much easier just to add,
not overwrite the method,
so $(document).ready
is very useful
and it is great if it can be implemented w/o jQuery.
To do this, you should add an event listener to DOMContentLoaded
1.
Here is an example:
1 2 3 |
|
DomContentloaded
means as in the literature,
when DOM is built.
As a matter of fact, $(document).ready
uses this internally
2.
Of course, addEventListener
can accept multiple methods3.
In these days, DOMContentLoaded
can be used in most of the browsers4,
so that $(document).ready
can be replaced by DOMContentLoaded
.
Multiple registration at onload
On the other hand, you may need to execute methods after loading all images.
It can be done by window.onload
, but only one method can be registered.
If you can use jQuery, it is easy that you can write like:
1 2 3 |
|
or
1 2 3 4 5 6 7 |
|
If you can’t use jQuery, you need to write like:
1 2 3 4 5 6 7 8 9 10 11 |
|
attacEvent
method is for IE.
Be careful that argument is a little different between addEventListener
and attachEvent
(load
and onload
, respectively)
5.
Most of browser can be covered by these two methods,
but if necessary, window.onload
can be placed here (though only one method can be registered).
Execution order
As written above, ready
is executed earlier than load
.
To check more details, let’s try following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
|
This code uses:
- load:
window.onload = function() {...
window.addEventListener("load", function() {...
$(window).load(function() {...
(jQuery)$(window).on("load", function() {...
(jQuery)$.event.add(window, "load", function() {...
(jQuery)
- ready:
document.addEventListener("DOMContentLoaded", function() {...
$(document).ready(function() {...
(jQuery)$(function() {...
(jQuery)
In addition, there are some immediately-invoked function expressions (IIFE).
The result with Google Chrome(43.0.2357.130, Mac) is:
IIFE 0
IIFE 1
IIFE 2
ready 0 $(document).ready(function() {...
ready 0 $(function() {...
ready 1 $(document).ready(function() {...
ready 1 $(function() {...
ready 0 $(document).on("ready", function() {...
ready 1 $(document).on("ready", function() {...
ready 0 document.addEventListener("DOMContentLoaded", function() {...
ready 1 document.addEventListener("DOMContentLoaded", function() {...
load 0 window.addEventListener("load", function() {...
load 0 $(window).load(function() {...
load 0 $(window).on("load", function() {...
load 0 $.event.add(window, "load", function() {...
load 1 $(window).load(function() {...
load 1 $(window).on("load", function() {...
load 1 $.event.add(window, "load", function() {...
load 1 window.onload = function() {...
load 1 window.addEventListener("load", function() {...
On the other hand, the result with Firefox(39.0, Mac) shows
almost same, but load 1 window.onload = function() {...
is shown before load 0 window.addEventListener("load", function() {...
.
After some tests, followings can be seen:
- About
ready
:- Frist, jQuery’s document ready related things, such
$(document).ready
, or$(function()
, are executed in the written order. - Second,
$(document).on("ready"
is executed. - Then,
"DOMContentLoaded"
is executed.
- Frist, jQuery’s document ready related things, such
- About
load
:- Only the last one of
window.onload
is executed. window.onload
andaddEventListener("load"
are executed in the written order. But if there are more than onewindow.onload
, the first one is used as an order point.- In above example,
load 1 window.onload
is written in the last, butload 0 window.onload
is used as an order point, therefore it is executed beforeload 1 window.addEventListener("load"
.
- In above example,
- Methods are executed in written order in jQuery’s methods,
$(window).load
,$(window).on("load"
and$event.add(window, "load"
. - On Firefox,
window.onload
andaddEventListener("load"
are executed before jQuery’sload
, if it is written before all of jQuery’sload
methods.- jQuery’s
load
is executed first if any of jQuery’sload
is written beforewindow.onload
. - Here, the order point of
window.onload
is the first written one as written above.
- jQuery’s
- On Google Chrome,
addEventListener("load"
is executed beore jQuerey’sload
, if it is written before all of jQuery’sload
method.- jQuery’s
load
is executed first if any of jQuery’sload
is written beforeaddEventListener("load"
.
- jQuery’s
- On Google Chrome,
window.onload
is executed before jQuery’sload
in any case.
- Only the last one of
It is a bit complex,
but one could argue that
it is unstable if these different methods are used all together,
so that it should be written in the same way in each load
or ready
.
-
javascript - $(document).ready equivalent without jQuery - Stack Overflow ↩
-
In the detail,
$(document).ready
is more complex (and powerful) that it emulatesDOMContentLoaded
in browsers which doesn’t have it (like old IE or Safari…). ↩ -
Usually,
addEventListener
is used with 3rd argument like:1
document.addEventListener('DOMContentLoaded', func, false);
Most of recent browsers set it
false
as default, so normally it is not necessary.Though in the old days like Firefox 6 requires 3rd argument, it is not necessary in these days.