JS file Loaded Jquery.js

Source: Internet
Author: User

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
  1. by firefoxmmx
  2. var script=document.createelement ("script");
  3. Script.type="Text/javascript";
  4. script.src="Jquery.js";
  5. document.getElementsByTagName (' head ') [0].appendchild (script);
  6. SetTimeout (function () {
  7. $ (document). Ready (function () {
  8. $ ("#bt"). Click (function () {
  9. Alert (' Hello World ');
  10. });
  11. });
  12. },100);


Where the 1.html code is as follows:

[HTML]View PlainCopy
  1. <html>
  2. <head>
  3. <script type="Text/javascript" src="1.js"></script>
  4. </head>
  5. <body>
  6. <input type="button" id= "bt" value="click" />
  7. </body>
  8. </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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.