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

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

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

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>IE9/10同時支持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
測試結(jié)果可以看出,IE9后已經(jīng)開始支持script的onload事件了。一直以來我們判斷js文件是否已經(jīng)加載完成就是用以上的兩個事件。很久以前就知道IE中使用onreadystatechange事件,事件handler中使用readyState的值判斷是否加載完成。其它瀏覽器使用onload事件。
復制代碼 代碼如下:

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

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

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

這種寫法的取巧之處在于onload和onreadystatechage都用同一個函數(shù),F(xiàn)irefox/Safari/Chrome/Opera中不支持onreadystatechage事件,也沒有readyState屬性,所以 !this.readyState 是針對這些瀏覽器。readyState是針對IE瀏覽器,載入完畢的情況是loaded,緩存的情況下可能會出現(xiàn)readyState為complete。所以兩個不能少。但由于IE9/10也已經(jīng)支持onload事件了,會造成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)文章

最新評論