Home > Web Front-end > JS Tutorial > body text

Only IE9/10 supports both onload and onreadystatechange event analysis of script elements_javascript skills

WBOY
Release: 2016-05-16 18:07:31
Original
1497 people have browsed it

As follows

Copy the code The code is as follows:


< html>


IE9/10 supports both the onload and onreadystatechange events of the script element

< ;/head>




Result:
IE6/7/8: Popup 2
IE9/10: Pop-up 2, 1
Firefox/Safari/Chrome/Opera: Pop-up 1
The test results show that IE9 has begun to support the script onload event. We have always used the above two events to determine whether the js file has been loaded. I have known for a long time that the onreadystatechange event is used in IE, and the value of readyState is used in the event handler to determine whether the loading is completed. Other browsers use the onload event.
Copy code The 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 writing method now. However, the following writing may cause the callback to be executed twice in IE9/10
Copy the code The code is as follows:

script.onload = script.onreadystatechange = function(){
if(!this.readyState || this.readyState == "loaded" || this.readyState == "complete"){
callback();
}
}

The trick of this writing method is that both onload and onreadystatechage use the same function. The onreadystatechage event is not supported in Firefox/Safari/Chrome/Opera , and there is no readyState attribute, so !this.readyState is for these browsers. readyState is for IE browser. When loading is complete, it is loaded. In the case of caching, readyState may be complete. So two cannot be less. However, since IE9/10 already supports the onload event, 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 labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template