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