動(dòng)態(tài)加載js的幾種方法
更新時(shí)間:2006年10月23日 00:00:00 作者:
1.直接document.write
<script language=javascript>
document.write(<script src='test.js'><\/script>);
</script>
2.動(dòng)態(tài)改變已有script的src屬性
<script src='' id=s1></script>
<script language=javascript>
s1.src=test.js
</script>
3.動(dòng)態(tài)創(chuàng)建script元素
<script>
var s2 = document.createElement(script);
s2.src=test.js
document.body.insertAdjacentElement(BeforeBegin,s2);
</script>
<head>
<script>
function loadJs(file){
var scriptTag = document.getElementById('loadScript');
var head = document.getElementsByTagName('head').item(0)
if(scriptTag) head.removeChild(scriptTag);
script = document.createElement('script');
script.src = file;
script.type = 'text/javascript';
script.id = 'loadScript';
head.appendChild(script)
}
</script>
</head>
<body onload=loadJs('1.js');loadJs('2.js')>
<script language=javascript>
document.write(<script src='test.js'><\/script>);
</script>
2.動(dòng)態(tài)改變已有script的src屬性
<script src='' id=s1></script>
<script language=javascript>
s1.src=test.js
</script>
3.動(dòng)態(tài)創(chuàng)建script元素
<script>
var s2 = document.createElement(script);
s2.src=test.js
document.body.insertAdjacentElement(BeforeBegin,s2);
</script>
<head>
<script>
function loadJs(file){
var scriptTag = document.getElementById('loadScript');
var head = document.getElementsByTagName('head').item(0)
if(scriptTag) head.removeChild(scriptTag);
script = document.createElement('script');
script.src = file;
script.type = 'text/javascript';
script.id = 'loadScript';
head.appendChild(script)
}
</script>
</head>
<body onload=loadJs('1.js');loadJs('2.js')>
相關(guān)文章
BootStrap智能表單實(shí)戰(zhàn)系列(三)分塊表單配置詳解
這篇文章主要介紹了BootStrap智能表單實(shí)戰(zhàn)系列(三)分塊表單配置詳解的相關(guān)資料,非常不錯(cuò)具有參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06javascript實(shí)現(xiàn)json頁(yè)面分頁(yè)實(shí)例代碼
這篇文章主要介紹了javascript實(shí)現(xiàn)json頁(yè)面分頁(yè)實(shí)例代碼,需要的朋友可以參考下2014-02-02JS組件系列之Bootstrap table表格組件神器【終結(jié)篇】
Bootstrap Table是輕量級(jí)的和功能豐富的以表格的形式顯示的數(shù)據(jù),支持單選,復(fù)選框,排序,分頁(yè),顯示/隱藏列,固定標(biāo)題滾動(dòng)表,響應(yīng)式設(shè)計(jì),Ajax加載JSON數(shù)據(jù),點(diǎn)擊排序的列,卡片視圖等。本文給大家介紹JS組件系列之Bootstrap table表格組件神器【終結(jié)篇】,一起學(xué)習(xí)吧2016-05-05js?剪切、復(fù)制、粘貼功能實(shí)現(xiàn)
Navigator.clipboard?API可以用來(lái)訪問系統(tǒng)剪貼板,可以實(shí)現(xiàn)【剪切、復(fù)制、粘貼】功能。該?API?被設(shè)計(jì)用來(lái)取代使用?document.execCommand()?的剪貼板訪問方式,不兼容?IE2023-05-05基于JS實(shí)現(xiàn)PHP的sprintf函數(shù)實(shí)例
這篇文章主要介紹了基于JS實(shí)現(xiàn)PHP的sprintf函數(shù)的方法,可實(shí)現(xiàn)JavaScript模擬PHPsprintf函數(shù)的輸出功能,涉及JavaScript字符串操作的相關(guān)技巧,需要的朋友可以參考下2015-11-11Layui 設(shè)置select下拉框自動(dòng)選中某項(xiàng)的方法
今天小編就為大家分享一篇Layui 設(shè)置select下拉框自動(dòng)選中某項(xiàng)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2018-08-08各種頁(yè)面定時(shí)跳轉(zhuǎn)(倒計(jì)時(shí)跳轉(zhuǎn))代碼總結(jié)
下面對(duì)實(shí)現(xiàn)頁(yè)面定時(shí)跳轉(zhuǎn)(也稱倒計(jì)時(shí)跳轉(zhuǎn))做一下總結(jié),以備不時(shí)之需,經(jīng)常使用的朋友可以參考下2013-10-10