2011-02-02 70 views
0

我想在腳本標記中加載2個外部JS腳本,有沒有辦法使用一個document.write標記加載2個腳本,還是必須聲明它兩次?似乎無法找到任何答案。使用document.write加載多個腳本

document.write('<script src="js/scroll.js"><\/script>'); 
document.write('<script src="js/mobile.js"><\/script>'); 
+0

您確定要使用document.write嗎? – Luke 2011-02-02 18:56:43

+0

我會避免`document.write`。如果使用`document.createElement`,則可以附加`onload`和`onreadystatechange`處理程序來檢測腳本何時完成加載。 – zzzzBov 2011-02-02 18:59:54

回答

1
document.write('<script src="js/scroll.js"><\/script><script src="js/mobile.js"><\/script>'); 
1

退房這樣:

//JS CODE: 
function include(path) 
{ 
    var e; 
    e = window.document.createElement('script'); 
    e.setAttribute('src',path); 
    window.document.body.appendChild(e); 
} 

include("js/scroll.js");  
include("js/mobile.js"); 
1

你可以使用jQuery?

jQuery("document").ready(function() { 

    if(navigator.platform == 'iPad' || navigator.platform == 'iPhone' || navigator.platform == 'iPod'){ 

    document.write('<script src="js/scroll.js"><\/script><script src="js/mobile.js"><\/script>'); 

    } 
});