0

I have one javascript program in my module; say test1.js. It has IF..ELSEIF statements. My challenge is I want to call different .js programs based on conditions in IF-ELSE statements.

TEST1.js looks like -

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

How do I do this in javascript?

2
  • 2
    What do your other JS files look like? Have you tried using functions? Commented Apr 22, 2014 at 9:23
  • it has few array variables and few functions. I mean there is javascript code in every .js file. I have created different .js file based on conditions as code in those files is somewhat large. if i inserted it under IF..ELSE statement then it may get messup Commented Apr 22, 2014 at 9:25

2 Answers 2

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);

    }
Sign up to request clarification or add additional context in comments.

Comments

0

test1.js

var arr={}; //array

test2.js

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

in HTML

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

Use this method.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.