首頁 >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