Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery - More than 1 "$(document).ready" = dirty code?

is it OK to use the

$(document).ready(function ()
{   

// some code

});

more than 1 time in the javascript code?

like image 433
Peter Avatar asked Sep 17 '10 08:09

Peter


People also ask

Can we have multiple document ready in jQuery?

We can have multiple document. ready() function in our code but only one body. onload() is allowed.

What does $( document .ready function () do?

$( document ).ready()A page can't be manipulated safely until the document is "ready." jQuery detects this state of readiness for you. Code included inside $( document ).ready() will only run once the page Document Object Model (DOM) is ready for JavaScript code to execute.

What is difference between $( document .ready function () vs $( function ()?

The key difference between $(document). ready() and $(window). load() event is that the code included inside onload function will run once the entire page(images, iframes, stylesheets,etc) are loaded whereas the $(document). ready() event fires before all images,iframes etc.

Why is document ready called twice?

ready event will fire twice if you have an exception that occurs within your statement.


2 Answers

Yes, it is OK, jQuery will queue and merge them into a single handler called when the DOM is ready.

like image 104
Darin Dimitrov Avatar answered Oct 26 '22 18:10

Darin Dimitrov


Sure it is ok. Sometimes you have no other option. especially when you have some included JS files with jQuery and some jQuery code in the page itself.

like image 32
Stefanvds Avatar answered Oct 26 '22 16:10

Stefanvds