Reputation: 2011
I've picked up an existing project from another developer and ive noticed in the code that they are executing js code within three different event handlers...
function pageLoad() {
//execute code
}
$(document).ready(function() {
//execute code
});
$(function() {
//execute code
});
My question is - arent they all exactly the same? Or at least the last two? I understand that pageLoad is called by the .NET framework so it's not dependent on the jQuery library having loaded like the second two are - that's my understanding anyway - is that about correct?
Upvotes: 23
Views: 30368
Reputation: 3338
$(document).ready()
will not fire for the partial postbacks (happened from AJAX). In that case, you should use MS AJAX
pageLoad function when you need to execute something when the page loads either from the full postback or partial.
The article given in the Encosia site is a good read.
Upvotes: 0
Reputation: 564
$(document).ready(function() {
`enter code here`
//execute code
});
$(function() {
`enter code here`
});
Both functions perform task in the same way, executes code inside them once the document is ready.
function pageLoad() {
`enter code here`
}
This executes after every post back, generally used with update panels controls as the above two functions does not execute every time or every post back.
Upvotes: 0
Reputation: 201
$(document).ready()
Ideal for one time initialization.
Optimization black magic; may run slightly earlier than pageLoad().
Does not re-attach functionality to elements affected by partial postbacks.
pageLoad()
Unsuitable for one time initialization if used with UpdatePanels.
Slightly less optimized in some browsers, but consistent.
Perfect for re-attaching functionality to elements within UpdatePanels.
Upvotes: 20
Reputation: 25465
The last one is just a shorthand notation of the one above it. http://www.jquery4u.com/dom-modification/types-document-ready/
Upvotes: 3
Reputation: 7133
pageLoad
and the jQuery ready
handler are both methods of accomplishing similar things.
The second two examples are identical.
http://encosia.com/document-ready-and-pageload-are-not-the-same/
Upvotes: 5