What is difference between document.ready() method and document.load() in javascript?

hi there

I have used these two methods a lot of time.but some time I have seen where unusual behaviour in web pages while calling functions or other parts Inside them.

I want to know what types of activity one should do in document.ready() and what is document.load()?

Thanks

document.ready() is the standard technique that’s used with jQuery, which waits until the DOM has been loaded before running your code. jQuery also has a shortcut where you pas the function directly to the jQuery object, which achieves the same thing:

$(function () {
    ...
});

document.load() waits for longer until after everything has been loaded, images and all. This one has been deprecated though due to the .on() technique being preferred instead.

$('document').on('load', function () {
    ...
});
3 Likes

if you put your scripts just before the </body> tag then you will never need to use ready as at that point it already is.

The only time you need load is if the script requires that other files load first (eg. a script to take action if images fail to load)

2 Likes

Thanks

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.