2012-07-12 114 views
1

我想知道如何導入.js文件內的另一個js文件就像我們如何導入內的另一個css文件css文件如何導入內的另一個js文件JS文件,就像我們如何導入CSS文件內的另一個CSS文件

all.css文件是這樣的.....

 @import "simple1.css"; 
    @import "simple2.css"; 
    @import "simple3.css"; 
    @import "simple4.css"; 

同樣的方式我想進口的js/jQuery的1.7.1.min.js在我custom.js文件

custom.js文件是這樣的

$(document).ready(function(){ 

    $("#AddImage").html("<img src='"+clientID+".jpg'/>"); 

    }); 

我不想包括JS/jQuery的1.7.1.min.js像

 <script type="text/javascript" src="js/jquery-1.7.1.min.js"></script> 

我想包括它在我custom.js文件。

請幫忙!提前致謝!!!

+0

http://stackoverflow.com/questions/950087/include-javascript-file-inside-javascript-file – 2012-07-12 11:37:04

+0

沒有它沒有爲我工作....是有一些簡單的代碼來導入它 – Christina 2012-07-12 11:39:05

回答

2

你可以像下面創建功能爲我工作。

function importJs(url) 
{ 
    var head = document.getElementsByTagName('head')[0]; 
    var script = document.createElement('script'); 
    script.type = 'text/javascript'; 
    script.src = url; 

    head.appendChild(script); 
} 

運行代碼,請參見下面

importJs("myScript.js"); 

例如現在,您可以在任何JS使用importJs()。您可以使用JS,CSS中類似@import

1

療法是這個插件http://code.google.com/p/jquery-include/。這個jQuery插件提供基於瀏覽器的文件,包括類似於SSI的文件。

$(document).includeReady(function() { 

     // initialisation code here 
     // e.g. modify include dom 

}); 

,並使用它

<span data-src="includes/test1.html"> 

     <p class="error-msg">include has not loaded</p> 

</span> 
2

該代碼使用jQuery

$.getScript("myscript.js"); 
相關問題