jquery $ (document). Ready () and onload loading sequence _javascript tips

Source: Internet
Author: User
Recently, when I changed a page embedded in a frame, I used jquery to do the effect, and the page itself was bound to the OnLoad event. After the change, Firefox test under normal fluency, IE will wait for a few seconds of jquery effect only appear, Day Lily is cold.

At first thought it was conflicting with the method that was loaded by itself onload. The common saying on the Internet is $ (document). Ready () is executed after page Dom parsing completes, and the OnLoad event is executed after all resources are ready to be completed, that is, $ (document). Ready () is to be executed before the onload, Especially when the page picture is larger, this time difference may be greater. But I this page is clearly the picture is displayed for more than 10 seconds, still do not see the effect of jquery out.

Delete the OnLoad loading method try, the result is the same, it seems not necessary to the original OnLoad event binding also use $ (document). Ready () to write. What is the reason that makes Firefox normal and IE can? Then debugging, found that the original binding of IE under the OnLoad method before the $ (document). Ready (), while Firefox executes the contents of $ (document). Ready (), then executes the original OnLoad method. This and the online statement does not seem to be exactly the same ah, oh, a little meaning, as if more and more close to the truth.

Flip the source of jquery to see $ (document). Ready () How to achieve it:
Copy Code code as follows:

if (jQuery.browser.msie && window = top) (function () {
if (Jquery.isready) return;
try {
Document.documentElement.doScroll ("left");
catch (Error) {
settimeout (Arguments.callee, 0);
Return
}
and execute any waiting functions
Jquery.ready ();
})();
JQuery.event.add (window, "load", jquery.ready);

The result is clear, ie only if the page is not embedded in the frame in the case of Firefox and so on, the first execution of $ (document). Ready (), and then execute the original onload method. For a page embedded in a frame, it is only bound to execute on the Load event, so it is naturally only after the original onload-bound method is executed. And this page just in the test environment there is no access to the resources, the more than 10-second delay is that it magnified the time lag.

Author: Joyce Liu

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.