在javascript嵌套时的执行顺序问题
[导读]先进行了测试,我们创建下面的测试环境。 文件 index.html 用于显示的页面 <!DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN http://www.w3.org/TR/html4/loose.dtd ><html><head>&...
先进行了测试,我们创建下面的测试环境。
文件 index.html 用于显示的页面
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>测试</title>
<script language="javascript">
alert('start');
document.write("<scr" + "ipt type='text/javascript' src='index.js'></scr" + "ipt>");
alert('end');
</script>
</head>
<body>
</body>
</html>
被 index.html 嵌入的 index.js
document.write("<scr" + "ipt type='text/javascript' src='index1.js'></scr" + "ipt>");
alert('index.js');
alert('index.js');
被 index.js 嵌入的 index1.js
alert('index1.js');
最终的显示提示的顺序:
start
end
index.js
index1.js
小结,在存在js嵌套的过程中,首先执行原先的代码,被嵌套的js中再执行。然后是下层嵌套代码的执行。
但是以上的部分只是限于了显示一个提示,如果需要证明这个推论是正确的,我想还是需要通过一些更多的代码来测试的。
我们只是修改index.js index1.js
index.js
var str = 'index.js'; document.write("<scr" + "ipt type='text/javascript' src='index1.js'></scr" + "ipt>"); alert('index.js'); alert(str); |
index1.js
alert(str); str = 'index1.js'; alert('index1.js'); alert(str); |
再次运行,顺序:
start
end
index.js
index.js
index.js
index1.js
index1.js
显然结果证明了我们上面的推论。
- 转载请注明来源:IT学习网 网址:http://www.t086.com/ 向您的朋友推荐此文章
- 特别声明: 本站除部分特别声明禁止转载的专稿外的其他文章可以自由转载,但请务必注明出处和原始作者。文章版权归文章原始作者所有。对于被本站转载文章的个人和网站,我们表示深深的谢意。如果本站转载的文章有版权问题请联系我们,我们会尽快予以更正。