Home  >  Article  >  Web Front-end  >  Several methods of dynamically loading js_javascript skills

Several methods of dynamically loading js_javascript skills

WBOY
WBOYOriginal
2016-05-16 19:25:021150browse

1. Direct document.write


2. Dynamically change the src attribute of the existing script



3. Dynamically create script elements




<script> <BR>document.write(<script src='test.js'><\/script>); <BR></script> <script></script><script> <BR>s1.src=test.js <BR></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>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn