2015-09-03 23 views

回答

3

在此基礎上回答How do I load a javascript file dynamically?

您可以從第一個動態加載第二個文件。這樣可以確保第一個完全加載。

// DOM: Create the script element 
var jsElm = document.createElement("script"); 
// set the type attribute 
jsElm.type = "application/javascript"; 
// make the script element load file 
jsElm.src = file; 
// finally insert the element to the body element in order to load the script 
document.body.appendChild(jsElm); 
2

既然你有兩個JS

<script type="text/javascript" src="1.js"></script> 
<script type="text/javascript" src="2.js"></script> 

2.js

document.getElementById("btn").onclick = function(){ 
    fn1(); 
} 

1.js

function fn1(){ 
alert("external fn clicked"); 
} 

OR


1.js:

function fn(){ 
    alert("Hello! HAPPY CODING"); 
} 

2.js:

$.getscript("1.js",function(){ 
fn(); 
}); 

希望這有助於...

相關問題