As follows:
Copy codeThe Code is as follows:
<! Doctype html>
<Html>
<Head>
<Meta charset = "UTF-8">
<Title> IE9/10 simultaneously supports onload and onreadystatechange events of script elements </title>
<Script src = "http://code.jquery.com/jquery.min.js" onload = "alert (1)" onreadystatechange = "alert (2)"> </script>
</Head>
<Body>
</Body>
</Html>
Result:
IE6/7/8: Pop Up 2
IE9/10: 2, 1
Firefox/Safari/Chrome/Opera: pop up 1
The test results show that IE9 has started to support onload events of scripts. We have been using the above two events to determine whether the js file has been loaded. A long time ago, I knew that the onreadystatechange event was used in IE, and the value of readyState was used in the event handler to determine whether the loading was complete. Other browsers use the onload event.
Copy codeThe Code is 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 method. However, the following code may allow the callback to be executed twice in IE9/10.
Copy codeThe Code is as follows:
Script. onload = script. onreadystatechange = function (){
If (! This. readyState | this. readyState = "loaded" | this. readyState = "complete "){
Callback ();
}
}
This method uses the same function for onload and onreadystatechage. The onreadystatechage event is not supported in Firefox, Safari, Chrome, or Opera, and the readyState attribute is not available! This. readyState is for these browsers. ReadyState is for IE browsers. After loading is completed, it is loaded. When caching, readyState may be complete. Therefore, there must be no less than two. However, because IE9/10 also supports onload events, the callback will be executed twice.
Related:
Http://www.w3.org/TR/html401/interact/scripts.html#h-18.2.1
Http://www.w3.org/TR/html5/scripting-1.html#script
Https://developer.mozilla.org/En/HTML/Element/Script