Skip to content Skip to sidebar Skip to footer

Jquery Document.ready Vs Pageload

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() {

Solution 1:

$(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.

Solution 2:

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/

Solution 3:

The last one is just a shorthand notation of the one above it. http://www.jquery4u.com/dom-modification/types-document-ready/

Solution 4:

$(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.

functionpageLoad() {
`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.

Solution 5:

$(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.

Post a Comment for "Jquery Document.ready Vs Pageload"