`

jQuery的load方法载入远程 HTML 文件的执行顺序

阅读更多
一个html文件在浏览器中的加载顺序是顺序进行的,如下文件:


<!DOCTYPE html>
<html>
<head>
	<title>a.html</title>
	
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="this is my page">
	<meta http-equiv="content-type" content="text/html; charset=UTF-8">
	
	<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
	<script type="text/javascript">
		alert('test');
	</script>
</head>
<body>
	This is my a HTML page.
	<button>funa</button>
	<br>
</body>

</html>



该文件直接在浏览器访问时,执行顺序是依次加载执行,先执行js中的alert,再加载body里面的内容。
不过用jQuery的load方法载入这个html文件时,该html的执行的顺序则是:首先加载完整个文档,再执行js的alert。
如果在上述html文件中加载一个js文件,<script type="text/javascript" src="test.js" charset="utf-8"></script>,变成:

<!DOCTYPE html>
<html>
<head>
	<title>a.html</title>
	
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="this is my page">
	<meta http-equiv="content-type" content="text/html; charset=UTF-8">
	
	<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
        <script type="text/javascript" src="test.js" charset="utf-8"></script>
	<script type="text/javascript">
		alert('test');
	</script>
</head>
<body>
	This is my a HTML page.
	<button>funa</button>
	<br>
</body>

</html>



而test.js的内容如下:
$(document).ready(function() {
	alert('onload');
});

那么load该html文件时,test.js里面的代码会执行吗?经测试,jQuery1.44会执行,jQuery1.72则不会执行。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics