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

Summary of methods for dynamically loading js_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:14:20895browse

The examples in this article summarize the methods of dynamically loading js. Share it with everyone for your reference. The details are as follows:

Method 1: Direct document.write (asynchronous)

Copy code The code is as follows:
");

Since this method is asynchronous loading, document.write will rewrite the interface, which is obviously not practical
Method 2: Dynamically change the src attribute of an existing script (asynchronous)

Copy code The code is as follows:

This method will not change the interface elements or rewrite the interface elements, but it will also be loaded asynchronously
Method 3: Dynamically create script elements (asynchronous)

Copy code The code is as follows:
<script><br> var body= document.getElementsByTagName('BODY').[0]; <br> var script= document.createElement("script"); <br> Script.type = "text/javascript"; <br> Script.src="xx.js"; <br> body.appendChild( oScript); <br> </script>

The advantage of this method compared to the second method is that there is no need to write a script tag in the interface at the beginning. The disadvantage is asynchronous loading

Method 4: XMLHttpRequest/ActiveXObject loading (asynchronous)

Copy code The code is as follows:
/**
* Load js script asynchronously
* @param id The id of the