2013-04-01 31 views
2

好讓說,我有這兩個環節如何將.js鏈接包含到另一個.js中?

<script src="http://code.jquery.com/jquery-latest.js"></script> 
<script type="text/javascript" src="http://dev.jquery.com/view/trunk/plugins/validate/jquery.validate.js"></script> 

我用這些作爲我的JavaScript源

,我創建了一個名爲

<script src="testing.js"></script> 

另一個文件,但在我的HTML我想顯示腳本src =「testing.js」我能夠以某種方式將前兩個鏈接放入testing.js文件中?

所以在我的HTML,它會只顯示

<script src="testing.js"></script> 

,但實際上它的運行三個腳本...

+1

告訴我們你寫在你的testing.js文件中的內容。 –

回答

0

您只能包含在HTML頁面中的腳本文件,而不是在另一個腳本文件

1

解決方案鏈接:

在你testing.js文件中創建這個函數:

function addJavascript(jsname,pos) { 
var th = document.getElementsByTagName(pos)[0]; 
var s = document.createElement('script'); 
s.setAttribute('type','text/javascript'); 
s.setAttribute('src',jsname); 
th.appendChild(s); 
} 

後來電話功能使用:

addJavascript('http://code.jquery.com/jquery-latest.js','head'); 
addJavascript('http://dev.jquery.com/view/trunk/plugins/validate/jquery.validate.js','head'); 

其他方法很少,但不是所有瀏覽器都兼容。

0

您可以使用requireJS進行JS文件的異步加載。如果這是一個選項,可以在這裏找到A tutorial to get you started

P.S.我知道這不是OP所要求的,但要求與此非常相似。

相關問題