How to dynamically load javascript files and load javascript files using javascript
How to dynamically load js files using javascript:
Sometimes we need to dynamically load js files as needed. This chapter will briefly introduce how to implement this function, hoping to bring some help to friends who need it.
I. Common loading methods:
<script type="text/javascript" src="mayi.js"></script>
The above is a common method for loading js files.
Ii. dynamically create <script> objects:
var head=document.getElementsByTagName("head")[0]; var script=document.createElement("script"); script.type="text/javascript"; script.src="mayi.js"; head.appendChild(script);
The above code can achieve dynamic loading of js files.
However, the above code needs to be supplemented because it is necessary to determine whether the js file has been loaded. The code is modified as follows:
var head= document.getElementsByTagName('head')[0]; var script= document.createElement('script'); script.type='text/javascript'; script.onload = script.onreadystatechange=function() { if(!this.readyState||this.readyState==="loaded"||this.readyState==="complete") { help(); script.onload = script.onreadystatechange=null; } }script.src='mayi.js'; head.appendChild(script);
The original address is: http://www.softwhy.com/forum.php? Mod = viewthread & tid = 8815.
For more information, see: http://www.softwhy.com/javascript/