MOCKSTACKS
EN
Questions And Answers

More Tutorials









Jquery Difference between $(document).ready() and $(window).load()


$(window).load() was deprecated in jQuery version 1.8 (and completely removed from jQuery 3.0) and as such should not be used anymore. The reasons for the deprecation are noted on the jQuery page about this event

Caveats of the load event when used with images

A common challenge developers attempt to solve using the .load() shortcut is to execute a function when an image (or collection of images) have completely loaded. There are several known caveats with this that should be noted. These are:

It doesn't work consistently nor reliably cross-browser
It doesn't fire correctly in WebKit if the image src is set to the same src as before
It doesn't correctly bubble up the DOM tree

Can cease to fire for images that already live in the browser's cache. If you still wish to use load() it is documented below:

$(document).ready() waits until the full DOM is availble -- all the elements in the HTML have been parsed and are in the document. However, resources such as images may not have fully loaded at this point. If it is important to wait until all resources are loaded, $(window).load() and you're aware of the significant limitations of this event then the below can be used instead:

$(document).ready(function() {
 console.log($("#my_large_image").height()); // may be 0 because the image isn't available
});
$(window).load(function() {
 console.log($("#my_large_image").height()); // will be correct
});


Conclusion

In this page (written and validated by ) you learned about Jquery Difference between document ready and window load . What's Next? If you are interested in completing Jquery tutorial, your next topic will be learning about: Jquery Difference between jQuery fn and executing your code before body.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.