Only IE9/10 supports onload and onreadystatechange event analysis of script elements.

Source: Internet
Author: User

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

Related Article

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.