简体   繁体   中英

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

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

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/

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

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

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

In this site you will find the difference :)

Document.ready Vs pageLoad

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM