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

熱點內容
網路中常用的傳輸介質 瀏覽:518
文件如何使用 瀏覽:322
同步推密碼找回 瀏覽:865
樂高怎麼才能用電腦編程序 瀏覽:65
本機qq文件為什麼找不到 瀏覽:264
安卓qq空間免升級 瀏覽:490
linux如何刪除模塊驅動程序 瀏覽:193
at89c51c程序 瀏覽:329
怎麼創建word大綱文件 瀏覽:622
裊裊朗誦文件生成器 瀏覽:626
1054件文件是多少gb 瀏覽:371
高州禁養區內能養豬多少頭的文件 瀏覽:927
win8ico文件 瀏覽:949
仁和數控怎麼編程 瀏覽:381
項目文件夾圖片 瀏覽:87
怎麼在東芝電視安裝app 瀏覽:954
plc顯示數字怎麼編程 瀏覽:439
如何辨別假網站 瀏覽:711
寬頻用別人的賬號密碼 瀏覽:556
新app如何佔有市場 瀏覽:42

友情鏈接