導航:首頁 > 編程語言 > 動態載入外部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文件路徑相關的資料

熱點內容
如何在手機里創建excel文件 瀏覽:172
電腦升級配置下載 瀏覽:44
蘋果系統鈴聲文件位置 瀏覽:663
又如何處理數據 瀏覽:44
文明5美麗新世界升級擋 瀏覽:30
數據源文件可以用什麼 瀏覽:614
fw文件找不到 瀏覽:192
蘋果沒法粘貼文件 瀏覽:643
rnn不能處理什麼數據 瀏覽:109
硬筆書法工具 瀏覽:357
文件的頭部和後部內容 瀏覽:917
50g的視頻文件刻錄大概多少錢 瀏覽:737
hbasejavaapi 瀏覽:983
cad每次關閉會自動保存一個文件 瀏覽:49
js點擊上滑下滑效果 瀏覽:191
怎樣在看過的pdf文件做標記 瀏覽:320
js數字轉時間 瀏覽:600
pdf文件不能在文件夾內預覽 瀏覽:837
angularjsapi中文 瀏覽:405
怎麼在兩個表格里找出相同的數據 瀏覽:650

友情鏈接