導航:首頁 > 編程語言 > 動態載入外部js文件路徑

動態載入外部js文件路徑

發布時間:2023-09-21 09:03:53

⑴ 如何在js文件中動態載入另一個js文件

1、直接來document.write
<script language="javascript">
document.write("<script src='test.js'><\/script>");
</script>

2、動態改變已有script的src屬性自
<script src='' id="s1"></script>
<script language="javascript">
s1.src="test.js"
</script>

3、動態創建script元素
<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>
其實原理就是利用dom動態的引入一個js到文件中來~就能和原有的js通信了~

⑵ 如何動態載入js文件

動態創建 script 標簽,就可以載入了,簡單寫個 demo,未測試,僅提供思路:


varscript=document.createElement('script');

script.type='text/javascript';

script.src='http://******************.js';

script.onload=function(){

console.log('Done');

};

document.getElementsByTagName('head')[0].appendChild(script);


上面代碼需要注意幾點:

⑶ 如何動態的載入js文件

1、直接document.write

document.write("<scriptsrc='test.js'></script>");

2、動態改變已有script的src屬性

<scriptsrc=''id="s1"></script>
<scriptlanguage="javascript">
s1.src="test.js"
</script>

3、動態創建script元素

<script>
varoHead=document.getElementsByTagName('HEAD').item(0);
varoScript=document.createElement("script");
oScript.type="text/javascript";
oScript.src="test.js";
oHead.appendChild(oScript);
</script>

這三種方法都是非同步執行的,也就是說,在載入這些腳本的同時,主頁面的腳本繼續運行,如果用以上的方法,那下面的代碼將得不到預期的效果。

4、原理:用XMLHTTP取得要腳本的內容,再創建 Script 對象。
注意:a.js必須用UTF8編碼保存,要不會出錯。因為伺服器與XML使用UTF8編碼傳送數據。

主頁面代碼:

<scriptlanguage="JavaScript">
functionGetHttpRequest()
{
if(window.XMLHttpRequest)//Gecko
returnnewXMLHttpRequest();
elseif(window.ActiveXObject)//IE
returnnewActiveXObject("MsXml2.XmlHttp");
}
functionAjaxPage(sId,url){
varoXmlHttp=GetHttpRequest();
oXmlHttp.OnReadyStateChange=function()
{
if(oXmlHttp.readyState==4)
{
if(oXmlHttp.status==200||oXmlHttp.status==304)
{
IncludeJS(sId,url,oXmlHttp.responseText);
}
else
{
alert('XMLrequesterror:'+oXmlHttp.statusText+'('+oXmlHttp.status+')');
}
}
}
oXmlHttp.open('GET',url,true);
oXmlHttp.send(null);
}
functionIncludeJS(sId,fileUrl,source)
{
if((source!=null)&&(!document.getElementById(sId))){
varoHead=document.getElementsByTagName('HEAD').item(0);
varoScript=document.createElement("script");
oScript.language="javascript";
oScript.type="text/javascript";
oScript.id=sId;
oScript.defer=true;
oScript.text=source;
oHead.appendChild(oScript);
}
}
AjaxPage("scrA","b.js");
alert("主頁面動態載入JS腳本。");
alert("主頁面動態載入a.js並取其中的變數:"+str);
</script>

⑷ 如何在js文件中動態載入另一個js文件

用document.write方法來實現。

在js文件中動態載入另一個js文件代碼及注釋步驟:

<html>
<body>

<scripttype="text/javascript">
document.write("<scriptsrc='要引用js'></script>");
</script>
<p>write方法的使用</p>
</body>
</html>

定義和用法

write() 方法可向文檔寫入 HTML 表達式或 JavaScript 代碼。

語法

document.write(exp1,exp2,exp3,....)

⑸ 如何在html頁面動態載入js文件

html引用外部js文件:<script type="text/javascript" src="js/index.js"></script>
其中src="js文件路徑"

閱讀全文

與動態載入外部js文件路徑相關的資料

熱點內容
win10勒索文件保護設置 瀏覽:842
arcgissde93安裝教程 瀏覽:487
xml文件注釋快捷鍵 瀏覽:878
extjs的配置文件怎麼配置重定向 瀏覽:740
access資料庫查看aspx 瀏覽:154
數控編程如何減少時間 瀏覽:779
蘋果FLAC屬性 瀏覽:642
硬碟評分工具 瀏覽:734
為什麼e福州app登不上 瀏覽:963
jsfoutputlink 瀏覽:472
哪個網站可以聽南音 瀏覽:264
蘋果裝系統裝win7驅動 瀏覽:686
php判斷file是否有文件 瀏覽:979
和平精英使用什麼編程開發 瀏覽:102
f3文件 瀏覽:523
快手3d環繞音樂用什麼app 瀏覽:376
linux新增一個文件 瀏覽:440
消失的手機圖片在哪個文件夾里 瀏覽:610
word2010表格外框雙線內框單線 瀏覽:56
powermill如何提高編程速度 瀏覽:465

友情鏈接