HTML Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsWeb DesignHTML Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 9th, 2000, 11:27 AM
kfellows kfellows is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2000
Posts: 2 kfellows User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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?

Reply With Quote
  #2  
Old November 9th, 2000, 10:50 PM
rycamor rycamor is offline
Gödelian monster
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jul 1999
Location: Pembroke Pines, Florida, USA
Posts: 2,300 rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 3 h 12 m 27 sec
Reputation Power: 53
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).]

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignHTML Programming > External .js files


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway