JS 的加载执行顺序

近来对JS在html中的加载顺序有点小疑惑,遂测试分析了一把:

<html>
	<head>
		<div>before head</div>
		<script type="text/javascript">
			var test = function() {
				alert("I'm in head!");
			}
			test();
		</script>
		<div>after head</div>
	</head>

	<body>
		<script type="text/javascript">
			var testPart1 = function() {
				alert("I'm in body-part1!");
			}
			testPart1();
		</script>
		<div>Just a test case...</div>
		<script type="text/javascript">
			var testPart2 = function() {
				alert("I'm in body-paer2!");
			}
			testPart2();
		</script>
		<script type="text/javascript">
			var testPart3 = function() {
				alert("I'm in body-part3!");
			}
		</script>
	</body>

	<script type="text/javascript">
		var testEnd = function() {
			alert("I'm in the end!");
		}
		testEnd();
	</script>
	<footer>you are in footer...<footer>
	<script type="text/javascript">
		alert("I'm behind of the footer!");
	</script>
</html>
结果依次为:div标签"before head"出现,"I'm in head!",div标签"after head"出现,"I'm in body-part1!", div "Just a test case..."出现,"I'm in body-part2!","I'm in the end!",footer标签"
you are in footer..."出现,"I'm behind of the footer!"。
说明了以下几点:
1) 按照html从上到下的顺序加载(并不是都先加载head然后加载body,如果head放在body后,就变成了先加载body后加载head,只不过习惯上head在前)。

2) html的tags和script脚本也是按照位置的先后顺序加载的。

3) script中如果只有函数定义而没有去触发,这个函数是不会被执行的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值