PDA

View Full Version : Passing A JavaScript Variable in href


RHK
10-26-2005, 09:03 PM
Hello, I'm a newbie with JavaScript and HTML, so please bear with me ;)

For ease of future modifications on my page, I want to have a list of URLs stored in an Array, and I want to then use these URLs in an href tag. Something like this:

<SCRIPT>
menuURL[0]="page2.htm";
menuURL[1]="javascript://";
...
</SCRIPT>

<BODY>
<A HREF = menuURL[0] ........


How is something like this possible, as well as being fairly crossplatform, and intuitive? I've changed the syntax, and tried various other things to no avail. Any help would be greatly appreciated. Thanks!

Lerura
10-26-2005, 10:37 PM
<script><!--
menuURL=new Array;
menuURL[0]="javascript:changeBG()"; // run a javascript;
menuURL[1]="test3.htm"; // go to a page;

function Page(which){
location.href=menuURL[which];
}

function changeBG(){
document.bgColor="blue";
}
// --></script>
<a href="javascript:Page(0)">link to menuURL[0]</a><br>
<a href="javascript:Page(1)">link to menuURL[1]</a>
will do.

ensure that what is written
menuURL[0]="here";
is exactly the same as if you had written it
<a href="here">Link</a>

PhotoJoe47
10-27-2005, 12:48 AM
<script><!--
menuURL=new Array;
menuURL[0]="javascript:changeBG()"; // run a javascript;
menuURL[1]="test3.htm"; // go to a page;

function Page(which){
location.href=menuURL[which];
}

function changeBG(){
document.bgColor="blue";
}
// --></script>
<a href="javascript:Page(0)">link to menuURL[0]</a><br>
<a href="javascript:Page(1)">link to menuURL[1]</a>
will do.

ensure that what is written
menuURL[0]="here";
is exactly the same as if you had written it
<a href="here">Link</a>

I like this example. I have saved it to my folder of test scripts.
I think I can build upon this for some future project.

Thanks,
PhotoJoe

RHK
10-27-2005, 05:04 PM
Thanks Lerura! :)