js動態引入的四種方式

來源:互聯網
上載者: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.   
  10.  <body>  
  11.     
  12.  </body>  
  13. </html>  

 

 

test.js

 

[html] view plaincopy 
  1. alert("hello! I am test.js");  
  2. var str="1";  




 

dynamic.js

 

[javascript] view plaincopy 
  1. //第一種方式:直接document.write 但這樣會把當前的頁面全覆寫掉  
  2. //document.write("<script src=‘test.js‘><\/script>");  
  3.   
  4.   
  5. //第二種方式:動態改變已有script的src屬性  
  6. //s1.src="test.js"  
  7.   
  8.   
  9. //第三種方式:動態建立script元素  
  10. /*    var oHead = document.getElementsByTagName(‘HEAD‘).item(0); 
  11.     var oScript= document.createElement("script"); 
  12.     oScript.type = "text/javascript"; 
  13.     oScript.src="test.js"; 
  14.     oHead.appendChild(oScript); 
  15. */  
  16. //其實原理就是利用dom動態引入一個js到檔案中來~就能和原有的js通訊了~  
  17.   
  18. //alert(str);  
  19.   
  20.   
  21. /*以上三種方式都採用非同步載入機制,也就是載入過程中,頁面會往下走, 
  22. 如果這樣的話會有問題的,如上面的str就訪問不到,因為當程式執行alert(str)時,test.js還在載入Ing.... 
  23.  
  24. <strong><span style="color:#ff0000;">那麼第四種就是基於ajax請求的,且是推薦</span></strong> 
  25. */  
  26.   
  27. function GetHttpRequest()  
  28. {  
  29.     if ( window.XMLHttpRequest ) // Gecko  
  30.         return new XMLHttpRequest() ;  
  31.     else if ( window.ActiveXObject ) // IE  
  32.         return new ActiveXObject("MsXml2.XmlHttp") ;  
  33. }  
  34.   
  35.   
  36. function ajaxPage(sId, url){  
  37.     var oXmlHttp = GetHttpRequest() ;  
  38.     oXmlHttp.onreadystatechange = function()    
  39.      {  
  40.         if (oXmlHttp.readyState == 4)  
  41.         {  
  42.            includeJS( sId, url, oXmlHttp.responseText );  
  43.         }  
  44.     }  
  45.     oXmlHttp.open(‘GET‘, url, false);//同步操作  
  46.     oXmlHttp.send(null);  
  47. }  
  48.   
  49.   
  50. function includeJS(sId, fileUrl, source)  
  51. {  
  52.     if ( ( source != null ) && ( !document.getElementById( sId ) ) ){  
  53.         var oHead = document.getElementsByTagName(‘HEAD‘).item(0);  
  54.         var oScript = document.createElement( "script" );  
  55.         oScript.type = "text/javascript";  
  56.         oScript.id = sId;  
  57.         oScript.text = source;  
  58.         oHead.appendChild( oScript );  
  59.     }  
  60. }  
  61.   
  62. ajaxPage( "scrA", "test.js" );  
  63. alert( "首頁面動態載入JS指令碼。");  
  64. alert( "首頁面動態載入a.js並取其中的變數:" + str );  

 

js動態引入的四種方式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.