1.直接document.write
<scriptlanguage=javascript>
document.write(<scriptsrc='test.js'></script>);
</script>
2.动态改变已有script的src属性
<scriptsrc=''id=s1></script>
<scriptlanguage=javascript>
s1.src=test.js
</script>
3.动态创建script元素
<script>
vars2=document.createElement(script);
s2.src=test.js
document.body.insertAdjacentElement(BeforeBegin,s2);
</script>
<head>
<script>
functionloadJs(file){
varscriptTag=document.getElementById('loadScript');
varhead=document.getElementsByTagName('head').item(0)
if(scriptTag)head.removeChild(scriptTag);
script=document.createElement('script');
script.src=file;
script.type='text/javascript';
script.id='loadScript';
head.appendChild(script)
}
</script>
</head>
<bodyonload=loadJs('1.js');loadJs('2.js')>
【动态加载js的几种方法】相关文章: