IE9/10 only the OnLoad and onReadyStateChange event analysis _javascript techniques that support script elements at the same time

Source: Internet
Author: User
As follows
Copy Code code as follows:

<! DOCTYPE html>
<meta charset= "Utf-8" >
<TITLE>IE9/10 also supports the onload and onReadyStateChange events for script elements </title>
<script src= "Http://code.jquery.com/jquery.min.js" onload= "alert (1)" Onreadystatechange= "alert (2)" ></ Script>
<body>
</body>

Results:
IE6/7/8: Eject 2
IE9/10: Pop-up 2,1
Firefox/safari/chrome/opera: Eject 1
The test results show that the IE9 has started to support the OnLoad event of the script. We have always judged whether the JS file has been loaded to complete is using the above two events. I knew it a long time ago. Using the onReadyStateChange event in IE, the value of readystate in the event handler is used to determine whether the load is complete. Other browsers use the OnLoad event.
Copy Code code as follows:

if (Isie) {
Script.onreadystatechange = function () {
if (this.readystate = = ' Loaded ' | | | this.readystate = = ' complete ') {
Callback ();
}
}
}else{
Script.onload = function () {
Callback ();
}
}

There is no problem with this writing now. But the following notation might allow a callback to be executed twice in IE9/10
Copy Code code as follows:

Script.onload = Script.onreadystatechange = function () {
if (!this.readystate | | this.readystate = = "Loaded" | | this.readystate = = "complete") {
Callback ();
}
}

The tricky thing about this writing is that both the onload and onreadystatechage use the same function, Firefox/safari/chrome/opera does not support onreadystatechage events, and there is no readystate attribute. So!this.readystate is for these browsers. ReadyState is for IE browser, loading the situation is loaded, cached in the case may appear readystate for complete. So two cannot be less. But since IE9/10 has also supported the OnLoad event, it will cause callback to execute 2 times.

Related:

Http://www.w3.org/TR/html401/interact/scripts.html

Http://www.w3.org/TR/html5/scripting-1.html

Https://developer.mozilla.org/En/HTML/Element/Script

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.