首页 >web前端 >js教程 >动态加载js的几种方法_javascript技巧

动态加载js的几种方法_javascript技巧

WBOY
WBOY原创
2016-05-16 19:25:021203浏览

1.直接document.write
<script> <BR>document.write(<script src='test.js'><\/script>); <BR></script>
2.动态改变已有script的src属性
<script></script>
<script> <BR>s1.src=test.js <BR></script>
3.动态创建script元素
<script> <BR>var s2 = document.createElement(script); <BR>s2.src=test.js <BR>document.body.insertAdjacentElement(BeforeBegin,s2); <BR></script> 


<script> <BR>function loadJs(file){ <BR>var scriptTag = document.getElementById('loadScript'); <BR>var head = document.getElementsByTagName('head').item(0) <BR>if(scriptTag) head.removeChild(scriptTag); <BR>script = document.createElement('script'); <BR>script.src = file; <BR>script.type = 'text/javascript'; <BR>script.id = 'loadScript'; <BR>head.appendChild(script) <BR>} <BR></script>



声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn