|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
How can I call an external .js file from within a function based on a condition?
I can use the following: <script language="javascript" src="myjsfil.js"></script> but how do I call it from within something like this: <script> if a==b { //call the external script here. } else { //do something else. } </no script> any ideas? |
|
#2
|
|||
|
|||
|
Sometimes you can use Javascript to write other Javascript. Try this example:
--test.html-- <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre><html> <body><script language="javascript"> var cond = 2; if(cond == 2){ document.write("<scri" + "pt language="javascript" src="myjsfil.js"></scri" + "pt>"); } else{ alert("NOOOPE"); } </script> </body> </html>[/code] --myjsfil.js-- <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>alert("YYEEPP");[/code] Just change the value of 'my_condition' to 3 and see the difference. The reason I break the <script> tag up into "<scri" + "pt>" is to keep the browser from trying to interpret it on the "first pass". but after it is written, the two fragments are concatenated together and the new script is evaluated. This is compatible with version 4 and up browsers, but I don't know if it will work with older browsers. However, the best way to deal with this situation is with server-side code, such as Perl, PHP, or ASP; only use the above method if you have no other alternative. [This message has been edited by rycamor (edited November 09, 2000).] |
![]() |
| Viewing: Dev Shed Forums > Web Design > HTML Programming > External .js files |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|