有三种方法可以实现,分别如下:
第一种、直接document.write:
<script language="javascript">
document.write("<script src='test.js'><\/script>");
</script>
第二种、动态改变已有script的src属性
<script src='' id="s1"></script>
<script language="javascript">
s1.src="test.js"
</script>
第三种、动态创建script元素:
<script>
var oHead = document.getElementsByTagName('HEAD').item(0);
var oScript= document.createElement("script");
oScript.type = "text/javascript";
oScript.src="test.js";
oHead.appendChild( oScript);
</script>
第一种、直接document.write:
<script language="javascript">
document.write("<script src='test.js'><\/script>");
</script>
第二种、动态改变已有script的src属性
<script src='' id="s1"></script>
<script language="javascript">
s1.src="test.js"
</script>
第三种、动态创建script元素:
<script>
var oHead = document.getElementsByTagName('HEAD').item(0);
var oScript= document.createElement("script");
oScript.type = "text/javascript";
oScript.src="test.js";
oHead.appendChild( oScript);
</script>