Are there any advantages of using the Jquery ready() function over window.onload?
// Jquery ready
$(document).ready(function() {
});
// window.onload
window.onload = function () {
}
Are there any advantages of using the Jquery ready() function over window.onload?
// Jquery ready
$(document).ready(function() {
});
// window.onload
window.onload = function () {
}
Depends on what you want to do.
DOMContentLoaded
).window.onload
(the load
event) runs after everything has finished loading. Images, Flash, and some scripts, but usually not stylesheets. Use this for code that should run only when the page won't change any more.Also, with window.onload
you can only attach one listener, but you can attach as many as you want with jQuery ready. To attach more than one event on window.onload
, use addEventListener
:
window.addEventListener('load', function () {
}, false);
Yes, window.onload allows you to have only one listener. jQuery ready attaches as many listeners as you want.
Windows.onload will wait for everything to load on a page including images. Document.ready will fire as soon as soon as the html is loaded.