欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

僅IE9/10同時(shí)支持script元素的onload和onreadystatechange事件分析

 更新時(shí)間:2011年04月27日 00:05:09   作者:  
測(cè)試結(jié)果可以看出,IE9后已經(jīng)開(kāi)始支持script的onload事件了。一直以來(lái)我們判斷js文件是否已經(jīng)加載完成就是用以上的兩個(gè)事件。
如下
復(fù)制代碼 代碼如下:

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>IE9/10同時(shí)支持script元素的onload和onreadystatechange事件</title>
<script src="http://code.jquery.com/jquery.min.js" onload="alert(1)" onreadystatechange="alert(2)"></script>
</head>
<body>
</body>
</html>

結(jié)果:
IE6/7/8 : 彈出2
IE9/10 : 彈出2,1
Firefox/Safari/Chrome/Opera : 彈出1
測(cè)試結(jié)果可以看出,IE9后已經(jīng)開(kāi)始支持script的onload事件了。一直以來(lái)我們判斷js文件是否已經(jīng)加載完成就是用以上的兩個(gè)事件。很久以前就知道IE中使用onreadystatechange事件,事件handler中使用readyState的值判斷是否加載完成。其它瀏覽器使用onload事件。
復(fù)制代碼 代碼如下:

if(isIE){
script.onreadystatechange = function(){
if(this.readyState == 'loaded' || this.readyState == 'complete'){
callback();
}
}
}else{
script.onload = function(){
callback();
}
}

這種寫(xiě)法現(xiàn)在也沒(méi)有問(wèn)題。但如下寫(xiě)法可能會(huì)讓的回調(diào)在IE9/10中執(zhí)行兩次
復(fù)制代碼 代碼如下:

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

這種寫(xiě)法的取巧之處在于onload和onreadystatechage都用同一個(gè)函數(shù),F(xiàn)irefox/Safari/Chrome/Opera中不支持onreadystatechage事件,也沒(méi)有readyState屬性,所以 !this.readyState 是針對(duì)這些瀏覽器。readyState是針對(duì)IE瀏覽器,載入完畢的情況是loaded,緩存的情況下可能會(huì)出現(xiàn)readyState為complete。所以?xún)蓚€(gè)不能少。但由于IE9/10也已經(jīng)支持onload事件了,會(huì)造成callback執(zhí)行2次。

相關(guān):

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 

相關(guān)文章

最新評(píng)論