Home > Web Front-end > JS Tutorial > body text

Several methods of dynamically loading js_javascript skills

WBOY
Release: 2016-05-16 19:25:02
Original
1162 people have browsed it

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>
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template