Four ways to introduce JS dynamically

Source: Internet
Author: User

Index.html

[HTML]View Plaincopy
  1. <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <meta content= "text/html;charset=utf-8" http-equiv="Content-type">
  5. <title> </title>
  6. <script src=' id="S1"></script>
  7. <script src="Dynamic.js"></script>
  8. </head>
  9. <body>
  10. </Body>
  11. </html>

Test.js

[HTML]View Plaincopy
    1. Alert ("Hello! I am Test.js ");
    2. var str="1";




Dynamic.js

[JavaScript]View Plaincopy
  1. The first way: direct document.write But this will put the current page full overwrite
  2. document.write ("<script src= ' test.js ' ><\/script>");
  3. Second way: Dynamically changing the SRC attribute of an existing script
  4. S1.src= "Test.js"
  5. Third Way: Dynamic creation of script elements
  6. /* var ohead = document.getelementsbytagname (' HEAD '). Item (0);
  7. var oscript= document.createelement ("script");
  8. Oscript.type = "Text/javascript";
  9. Oscript.src= "Test.js";
  10. Ohead.appendchild (Oscript);
  11. */
  12. In fact, the principle is to use the DOM dynamic introduction of a JS to the file to ~ can and the original JS communication ~
  13. alert (str);
  14. /* More than three ways to use the asynchronous loading mechanism, which is the loading process, the page will go down,
  15. If this is problematic, such as the above STR is inaccessible, because when the program executes alert (STR), test.js is still loading ing ....
  16. <strong><span style= "color: #ff0000;" > So the fourth is based on Ajax requests, and is recommended </span></strong>
  17. */
  18. function Gethttprequest ()
  19. {
  20. if (window. XMLHttpRequest) //Gecko
  21. return new XMLHttpRequest ();
  22. Else if (window. ActiveXObject) //IE
  23. return new ActiveXObject ("msxml2.xmlhttp");
  24. }
  25. function ajaxpage (sId, url) {
  26. var oxmlhttp = Gethttprequest ();
  27. Oxmlhttp.onreadystatechange = function ()
  28. {
  29. if (oxmlhttp.readystate = = 4)
  30. {
  31. Includejs (sId, URL, oxmlhttp.responsetext);
  32. }
  33. }
  34. Oxmlhttp.open (' GET ', URL, false); Synchronous Operation
  35. Oxmlhttp.send (null);
  36. }
  37. function Includejs (sId, FILEURL, source)
  38. {
  39. if (source = null) && (!document.getelementbyid (sId))) {
  40. var ohead = document.getelementsbytagname (' HEAD '). Item (0);
  41. var oscript = document.createelement ( "script");
  42. Oscript.type = "Text/javascript";
  43. Oscript.id = sId;
  44. Oscript.text = source;
  45. Ohead.appendchild (Oscript);
  46. }
  47. }
  48. Ajaxpage ( "SCRA", "test.js");
  49. Alert ( The main page dynamically loads the JS script.  ");
  50. Alert ( "The main page dynamically loads the A.js and takes the variables:" + str);

Four ways to introduce JS dynamically

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.