Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between $window.load and window.onload?

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?

like image 661
Joshua Dance Avatar asked Mar 04 '14 21:03

Joshua Dance


1 Answers

.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(){});
like image 132
Kevin B Avatar answered Sep 21 '22 12:09

Kevin B