Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to load javascript from javascript with condition.

Status
Not open for further replies.

cote

IS-IT--Management
May 17, 2002
10
0
0
US
HELP!

I know how to load a javascript without any condition such as <script ... SRC:&quot;myjavascripttobeloaded.js&quot; />

I need to load a javascript depending on the value of variable. Here the sample pseudo-code.

<script language=&quot;javascript1.2&quot;>
var tmp = bla...
if (tmp == 1) {I want to load jsfile1.js}
if (tmp == 2) {I want to load jsfile2.js}
if (tmp == 3) {I want to load jsfile3.js}
so on and so on.

Can someone help please. Thanks.


</script>
 
You can make a function for each case, but you can't use the <script> tag inside the javascript because it's an unknown command 4 javascript.

<script language=&quot;javascript1.2&quot;>
<!--
function case1() {}
function case2() {}
function case3() {}
var x = 2;
if(x == 1) {call function case1}
else if(x == 2) {call function case2}
else if(x == 3) {call function case3}
//-->
</script>
 
Hi cote,

read this page;

The question of this answer is:
How can I prevent an external JavaScript file from loading until called for by the click of a button?

hope this helps,
Erik <!-- My sport: Boomerang throwing !!
This year I will participate at the World Championships in Germany. (!! Many Happy Returns !! -->
 
Thanks for your help, but I still have problem. I tried the solution from DuskMate but it doesn't work. Do I miss something? I keep getting error. The answer from Boomerang will require me to have many html file. The idea is to have only one html file that can load certain javascript file.

My code:
Assuming I have AAA.js and BBB.js files and each contain 1 line var theValue = '??'; ==>?? is AA in AAA.js and BB in BBB.js.

<html><head><title></title>
function calla() {
<script language=&quot;javascript1.2&quot;
src=&quot;AAA.js&quot;
type='text/javascript'>
}
function calla() {
<script language=&quot;javascript1.2&quot;
src=&quot;BBB.js&quot;
type='text/javascript'>
}
</head>
<body>
var test = 'a';
if (test == 'a') {calla;}
</body></html>
 
I received some complaints from AOL viewers about loading JS files dynamically like this because their version (who knows WHAT that might be) of the AOL browser choked on the dynamic file loading. To be safe, you'd be best off doing this in ASP or PHP, or some other server side scripting, so the client's computer ONLY gets what you want. What you described would be best done with just a list of the appropriate values rather than the extra overhead of a variety of external JS files.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top