IE下的异步JS测试
test.html如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head id="head">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>test</title>
<style type='text/css'>
img
{
border: 1px solid red;
display: block;
height: 50px;
width: 50px;
}
</style>
<script type='text/javascript'>
//因为有些函数需要传参,所以将参数带进去
function Exec(funcName,arr) {
if(typeof funcName =="string")
{
//如果此函数已经被加载
if (window[funcName]) {
window[funcName](arr);
} //如果函数没有被加载,则每隔0.1秒检查一次,直到加载完成
else {
window.setTimeout(function() { Exec(funcName,arr) }, 1000);
}
}else
{
funcName(arr);
}
}
//可以这样调用:Exec(window.fun||"fun",[456]);
function AppendJS(src, func) {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = src;
if (script.readyState) {
script.onreadystatechange = function() {
if (this.readyState == "loaded" || this.readyState == "complete") {
if (func) {
func();
}
}
};
} else {
if (func) {
script.onload = func;
}
}
document.getElementById('head').appendChild(script);
}
AppendJS("jquery-1.4.2.min.js", function() {
// Fuck(); 这一句报错的几率很大,因为jquery-1.4.2.min.js加载完成了,但1.js可能还未加载完成,所以Fuck函数可能还未定义
Exec(window.Fuck || "Fuck");//这一句不会报错,因为做了判断
});
AppendJS("1.js", null);
</script>
</head>
<body id="bd">
<div id="dd">
</div>
</body>
</html>
1.js如下:
for (var i = 0; i < 2000000; i++) {
}
for (var i = 0; i < 1000000; i++) {
}
for (var i = 0; i < 100000; i++) {
}
function Fuck() {
alert("fuck");
}