Original link: http://blog.csdn.net/whatday/article/details/39553451
There is a need recently:
1. In an HTML can only introduce a JS file can not have JS code and other JS file introduction
2. This JS file also introduces other JS files
3. All JS functions are written in this JS file, the code uses jquery-related stuff, so the first thing to do here is how to introduce jquery.js.
Search on the Internet a lot of methods are not very practical, because I left the web for many years and finally asked a friend to get the following code
1.js
[JavaScript]View PlainCopy
- by firefoxmmx
- var script=document.createelement ("script");
- Script.type="Text/javascript";
- script.src="Jquery.js";
- document.getElementsByTagName (' head ') [0].appendchild (script);
- SetTimeout (function () {
- $ (document). Ready (function () {
- $ ("#bt"). Click (function () {
- Alert (' Hello World ');
- });
- });
- },100);
Where the 1.html code is as follows:
[HTML]View PlainCopy
- <html>
- <head>
- <script type="Text/javascript" src="1.js"></script>
- </head>
- <body>
- <input type="button" id= "bt" value="click" />
- </body>
- </html>
If you want to test also need to add jquery.js this can be self-download
Click button Effect after running
Here are some more online methods:
1. Direct Document.write<script language= "JavaScript" > document.write ("<script src= ' test.js ' ><\/" Script> ") </script>2, dynamically changing the src attribute of the script <script src=" id= "S1" ></script><script language= " JavaScript "> s1.src=" test.js "</script>3, dynamically create script element <script> var ohead = document.getElementsByTagName (' HEAD '). Item (0); var oscript= document.createelement ("script"); Oscript.type = "Text/javascript"; Oscript.src= "Test.js"; Ohead.appendchild (Oscript);</script> In fact, the principle is to use the DOM dynamic introduction of a JS to the file to ~ can and the original JS communication ~
JS file Loaded Jquery.js