faqts : Computers : Programming : Languages : JavaScript : Links

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

9 of 11 people (82%) answered Yes
Recently 6 of 8 people (75%) answered Yes

Entry

Need link to have variable in it www.aa.com/variable/index.html

May 3rd, 2001 05:15
Colin Fraser, Linus Brown,


Not knowing the conditions under which you want to substitute the 
variable makes it a little difficult to assess how to approach this 
particular problem, but here is one way. (It may also be why noone else 
has been prepared to have a go at this question) 
<script language="javascript">
pathname=new Array()
pathname[0]="mine/coal"
pathname[1]="oil/pollution"
pathname[2]="companies/profits"
/*to make sure the array is populated, to be removed when function is 
running.  */
for (i=0; i<=2; i=i+1)
           {
         document.write(" The name of the server is 
http://www.whatserver/"+pathname[i]+"/index.htm<br>")
	}
function getpath(i) {
   location.href="http://www.whatserver/"+pathname[i]+"/index.htm"
   }
</script>
<A href="javascript:getpath('0')">First</a><br>
<A href="javascript:getpath('1')">Second</a><br>
<A href="javascript:getpath('2')">Third</a><br>
This has been given a cursory test and it does work. However, I suspect 
that this is the sort of coding trickery that creates more problems than 
it solves. If you want to set up a clicking cue that is dynamic, and 
using a variable, that might take a little bit of doing, and be error 
prone then. This may save something in time however, when you are 
writing the pages, one link and just fill in the blank, but setting it 
up seems to be a bit of an effort, particularly if you have only a few 
links. If you have lots of links then this may be useful.    
To set up something a little more dynamic maybe you may want to use 
something like : 
if (browser.appname=="Netscape") {
         document.write(" <A 
href=javascript:getpath('0')>Netscape</a><br>") }
else if (browser.appname=="Microsoft") {
         document.write(" <A 
href=javascript:getpath('1')>Microsoft</a><br> " ) }
else if (browser.appname=="Opera") {
         document.write(" <A href=javascript:getpath('2')>Opera</a><br> 
" ) }
else 
        document.write(" <A href='javascript:getpath('3')'>Other</a><br> 
" )
This has not been tested, I only thought of it here. There are problems 
with this too,  make sure you have enough elements in the array for 
starters. But make sure the browser types and names are correct. This 
may be OK for sending browsers to optimized pages, but have you got the 
time to set up and optimize that many pages, let alone the server space 
to run them?  The one thing that JavaScript desparately needs is a 
Select..Case statement.