Document.ready multiple times or once

Hi all

There are two sets of codes written below.

Does writing “Document.ready function” multiple times or once make a difference in the performance of how fast the code will work.

In First set “document.ready function” is written multiple times at different sections of page.


$(document).ready(function() {

    // some code comes here

});

$(document).ready(function() {

    // some code comes here

});

$(document).ready(function() {

    // some code comes here

});

$(document).ready(function() {

    // some code comes here

});

In the second set “document.ready function” is written only once.


$(document).ready(function() {

    // some code comes here

    // some code comes here

    // some code comes here

    // some code comes here

});

Thanks
Vineet

I would assume it makes no difference. but the browsers’ dev tools nowadays come with at least a simple profiling tool, so you could check that.

I agree.

Multiple $(document).ready() calls do however, suggest that your code is not organized as well as it could be. I would avoid doing this. In fact, if you place all of your JS in the correct position (before the closing <body> tag), then it is often the case that using $(document).ready() is not necessary at all.

Thanks Dormilich and Pullo

I will try to put to all together at one place so that it stays organised

Vineet

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