Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - is it bad to have multiple $(document).ready(function() {});

Tags:

jquery

ajax

Is it bad to have multiple $(document).ready(function() {}); on your page? I have a website where I load different things at different times. I fire off those partial postback functions inside $(document).ready() but i have about 4 or 5 on the page at once. Is this a bad practice? Specifically, will it cause any performance issues?

like image 272
codette Avatar asked Jul 18 '09 18:07

codette


People also ask

Can you have multiple $( document .ready function ()?

ready' function in a page? Can we add more than one 'document. ready' function in a page? Yes we can do it as like I did in below example both the $(document).

How many times can we use $( document ready () in page in JavaScript?

As many times as you like. They fire in order of declaration. $(document). ready() will fire when the document is ready (when it's all loaded by the browser).

What is the use of multiple document ready function in jQuery?

Multiple $(document). It's great to be able to group your functions within a file or even across multiple files, and jQuery's flexible $(document). ready() function allows you to do that, pain free.

Can you have nested document ready function?

Yes. you are right. The behaviour of second snippet is same with jQuery 2.1.


1 Answers

This answer is no longer relevant. Please see other posts below for more up-to-date jQuery $.ready() impacts. This post is over 3 years old.

See: http://jsperf.com/docready/11

The answer is no! You can litter them as much as you want (note the word litter). They just become a queue of events that are called when the ready event is triggered.

http://www.learningjquery.com/2006/09/multiple-document-ready

like image 198
Justin Van Horne Avatar answered Sep 20 '22 01:09

Justin Van Horne