Transfer: https: //www.cnblogs.com/tracylin/p/5122175.html
Another one can learn: https: //blog.csdn.net/wnvalentin/article/details/79769393
Wherein the dynamic scenario Tagging techniques Section 2.3, as follows:
Paste the code first:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script type="text/javascript" src="./test1.js"></script> </head> <body> <script type="text/javascript">
callback"I am a Alert (callback () {function") } function loadScript(url,callback){ var script = document.createElement("script"); script.type = "text/javascript"; //绑定加载完毕的事件 if(script.readyState){ script.onreadystatechange = function(){ if(script.readyState === "loaded" || script.readyState === "complete"){ the callback && the callback (); } } } the else { script.onload = function () { the callback && the callback (); } } script.src = URL; document.getElementsByTagName ( " head " ) [ 0 ] .appendChild (Script); }
the loadScript ( " ./test.js " , the callback) // this function is called and executed to load the code immediately test.js
</script> </body> </html>
test.js is a js file in the same folder, the contents of only one js code: alert ( "I'm test.js")
test1.js is the same folder js files, the content is only one js code: alert ( "I'm test1.js")
Open the top of the HTML file in a browser, the result would be:
1, pop-up "I'm test1.js"
2, pop-up "I'm test.js"
3, pop-up "I'm a callback"
As can be seen, from the execution code at first performed the <script type = "text / javascript" src = "./ test1.js"> </ script>, put test1.js downloaded and executed (performed synchronously ), and then execute the code in the body of the script tag, call loadScript this execution, the case will test.js downloaded, and then added to a script tag to the head, followed by the execution test.js code, then the callback function is executed
So call loadScript see where it is and where to download and execute the introduction of external js file