This might be a rookie question, but I have searched and tried a lot. What the is difference between window.onload
and $window.load
?
There are great answers about the difference between window.onload and document.ready and document.onload vs window.onload etc, but I have not found a resource or article that mentions both .onload
and .load
.
jQuery documentation says that .load is
"This method is a shortcut for .on( "load", handler )."
I tried putting both window.onload
and window.load
on the page and seeing which gets hit first or if they both get hit but they seem to interfere with each other.
(window).onload(function(){
alert("window onload - executes when the window's load event fires.");
}
$(document).ready(function(){
alert("document is ready - executes when HTML-Document is loaded and DOM is ready");
}
$(window).load(function(){
alert("window is loaded - executes when complete page is fully loaded, including all frames, objects and images");
}
What are the differences between them and why would you use one over the other?
.load as an event binding method has been removed as of jquery 1.9, as noted in the documentation you linked to (it's located in the deprecated section)
Therefore, the difference is one does what you want
window.onload = function(){};
and the other does nothing
$(window).load(function(){}); // does nothing!
an alternative way of writing the first one is:
$(window).on("load",function(){});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With