动态加载js的几种方法
2018-10-15 18:05
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
</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)>