公司名称:上海旭强信息科技有限公司

公司电话:(021)-51872701
      (021)-51302668
      (021)-51302669

公司传真:(021)-51302669-21

公司网址:http://www.51site.net.cn

通讯地址:上海市浦东大道1097号14-19E

邮政编码:200135

公司周边公交线路:

619路、774路、570路、610路、779路、639路、573路、787路、陆川专线、申高专线、沪合专线、 新川专线、旅游三线路

到 源深路站 或 桃林路 或 五牛城站 下车

首页 | 关于我们 | 网站建设 | 网站推广 | 电子商务 
 

在javascript嵌套时的执行顺序问题

2008-1-4 14:31:19

先进行了测试,我们创建下面的测试环境。
 
文件 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');
 
被 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.itlearner.com/article/2007/3926.shtml

责任编辑:
相关文章
 
  Copyright © 2007 上海旭强信息科技有限公司版权所有