Reputation: 6147
How many $(document).ready()
function can we use in a single html file or in a single javascript file?
$(document).ready(function(){
alert("first document ready");
//do some action here
});
$(document).ready(function(){
alert("second document ready");
//do some action here too
});
If we can use infinite, how will they be invoked? Will they execute line by line or is there any algorithm for calling this?
Upvotes: 3
Views: 1486
Reputation: 6147
I found a very nice answer about it in stackoverflow itself
https://stackoverflow.com/a/1327766/1225190
Here is some more reference about it
Tutorials:Multiple $(document).ready()
Upvotes: 0
Reputation: 150040
"If we can use infinite, how will they be invoked? Will they execute line by line or is there any algorithm for calling this?"
You obviously can't attach a literally infinite number of handlers, but there's no specific limit.
jQuery will execute the handlers in the order they are attached - behind the scenes jQuery binds its own handler to the event once, and keeps a list of all the handlers you've supplied.
This event-handling proxy concept applies to handlers for other events bound with jQuery, not just document ready, though document ready has a special case that if you try to bind a ready handler after the document is ready it will be executed "immediately" (not really "immediately" because it is done via a setTimeout()
call so that it is done asynchronously, but near enough).
Upvotes: 0
Reputation: 103348
There is no limit to some extent (until performance becomes an issue). They are just event listeners for when the document is ready.
Its not the same as say Page_Load
event in ASP.NET C#
I generally limit to one "document ready" function per javascript file however, for organisation purposes.
Upvotes: 2
Reputation: 953
We can attach many listeners to same event of java script. No maximum limits until the execution start causing script timeout. :). Java script is single threaded by default. So one by one execution will occur. I guess the order should be the order in which we attach event. But not sure. Good question. Need to try this to see the order.
Upvotes: 1
Reputation: 12184
UNLIMITED.
I suppose you can have multiple of them and all of them will be executed one by one when the document is ready. I tried the same for click event and it works.
But why would you do such a thing ?
Upvotes: 0
Reputation: 3474
You can define as many listeners on document.ready
as you like. The event will be fired once, and every listener called once.
However the order of attachment to the event does not guarantee the order of execution. There's no promise that the second document.ready
will fire after the first.
Upvotes: 1
Reputation: 94469
You can use as many $document.ready()
functions as you would like. See this explanation.
Despite this ability, I would limit the use of this function to as few times as possible, to avoid scattering your script that executes on page load across the application.
Upvotes: 1
Reputation: 12683
Well, I believe 'As much as you want'. But I would prefer to keep my code neat.
Upvotes: 2