2014-04-22 75 views
0

我的模塊中有一個JavaScript程序;說test1.js。它有IF..ELSEIF陳述。我的挑戰是我想根據IF-ELSE聲明中的條件調用不同的.js程序。從另一個.js文件調用.js文件

TEST1.js樣子 -

----- 
if(cond1) { 
    //want to call test2.js here 
} 
else if(cond2) { 
    //want to call test3.js here 
} 

如何在JavaScript這樣做呢?

+2

你的其他JS文件是什麼樣的?你嘗試過使用函數嗎? – Scimonster

+0

它有很少的數組變量和很少的功能。我的意思是在每個'.js'文件中都有javascript代碼。我根據條件創建了不同的'.js'文件,因爲這些文件中的代碼有點大。如果我將它插入在'IF..ELSE'語句下,那麼它可能會搞亂 –

回答

1
if(cond1) 
    { 
     //want to call test2.js here 
      var js = document.createElement("script"); 
      js.type = "text/javascript"; 
      js.src = "test2.js"; 
      document.body.appendChild(js); 
    } 
    else if(cond2) 
    { 
      //want to call test3.js here 

      var js = document.createElement("script"); 
      js.type = "text/javascript"; 
      js.src = "test3.js"; 
      document.body.appendChild(js); 

    } 
+0

謝謝!有效 .. –

0

test1.js

var arr={}; //array 

test2.js

function alertOne() { 
    if(cond1) { 
    //from test1.js here 
    alert(arr); 
    } 
    else if(cond2) { 
    } 
} 

在HTML

<head> 
    <script src="test1.js" type="text/javascript"></script> 
    <script src="test2.js" type="text/javascript"></script> 
<head> 

使用此方法。

相關問題