标签:
1 <html> 2 <head> 3 </head> 4 <body> 5 xxxxxxxxxxxxxxx 6 <h1>推迟加载</h1> 7 <script type="text/javascript" src="a.js"></script> 8 <link href="a.css" rel="stylesheet" /> 9 </body> 10 </html>
1 alert("已暂停加载") 2 alert("开始加载")
1 <script type="text/javascript" src="a.js" async></script>
1 setTimeout("alert(‘异步加载成功‘)",2000) //测试IE时候a.js文件要修改
1 <script type="text/javascript"> 2 (function() { 3 var s = document.createElement(‘script‘); 4 s.type = ‘text/javascript‘; 5 s.async = true; //这句可以删除,但是效果不变。 6 s.src = ‘js/a.js‘; 7 var x = document.getElementsByTagName(‘script‘)[0]; 8 x.parentNode.insertBefore(s, x); 9 })(); 10 </script>
1 <script type="text/javascript"> 2 window.onload=function(){ 3 document.getElementById(‘div‘).innerHTML="onload完成" 4 } 5 </script>
6 //写在html中
1 (function() { 2 function async_load(){ 3 var s = document.createElement(‘script‘); 4 s.type = ‘text/javascript‘; 5 s.async = true; 6 s.src = ‘js/yibujiaz.js‘; 7 var x = document.getElementsByTagName(‘script‘)[0]; 8 x.parentNode.insertBefore(s, x); 9 } 10 if (window.attachEvent) 11 window.attachEvent(‘onload‘, async_load); 12 else 13 window.addEventListener(‘load‘, async_load, false); 14 })();
1 // setTimeout("alert(‘异步加载成功‘)",2000) 2 // alert("已暂停加载") 3 // alert("开始加载")
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>异步加载</title>
<!-- <script type="text/javascript" src="js/yibujiaz.js" async></script> -->
<script type="text/javascript">
(function() {
var s = document.createElement(‘script‘);
s.type = ‘text/javascript‘;
s.src = ‘js/yibujiaz.js‘;
var x = document.getElementsByTagName(‘script‘)[0];
x.parentNode.insertBefore(s, x);
})();
</script>
<!-- <script type="text/javascript">
(function() {
function async_load(){
var s = document.createElement(‘script‘);
s.type = ‘text/javascript‘;
s.async = true;
s.src = ‘js/yibujiaz.js‘;
var x = document.getElementsByTagName(‘script‘)[0];
x.parentNode.insertBefore(s, x);
}
if (window.attachEvent)
window.attachEvent(‘onload‘, async_load);
else
window.addEventListener(‘load‘, async_load, false);
})();
</script>
<script type="text/javascript">
window.onload=function(){
document.getElementById(‘div‘).innerHTML="onload完成"
}
</script> -->
<style type="text/css">
div{
width:200px;
height:200px;
background-color: blue;
margin:0 auto;
}
</style>
</head>
<body>
<div id="div">
</div>
</body>
</html>
标签:
原文地址:http://www.cnblogs.com/lengxiaobao/p/5766627.html