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!

help me with; strings, functions and div boxes

Status
Not open for further replies.

danbhala

Technical User
Jun 2, 2004
2
0
0
IE
hi,
basically i have two html pages. on one page i have links that all point to the other page that pass an extra bit of information:

<a href="agencies.html#Agent1">1</a>
<a href="agencies.html#Agent2">2</a>
<a href="agencies.html#Agent3">3</a>etc...

on the other page i have this code to grab the infomation after the '#' charecter:

var loc=document.location.href;
var agency;
agency=loc.substring(loc.indexOf("#")+1,loc.length);

So basically the variable 'agency' holds a string; 'Agent1' or 'Agent2' or 'Agent3' etc...

i also have functions called Agent1(),Agent2(),Agent3()etc. that set information into div boxes throughout my web page.

here is an example of on of the agent functions:

function Agent1(){
document.getElementById('Div1').innerHTML='Info..';
document.getElementById('Div2').innerHTML='Info..';
document.getElementById('Div3').innerHTML='Info..';
}

in the body the divs are like this:
<div id="Div1"></div>
<div id="Div2"></div>
<div id="Div3"></div>


How can i, depending on what link i clicked call the particular function to set the correct agent information needed?

i.e. if i click the link:
<a href="agencies.html#Agent1">1</a>

it will call function:

function Agent1(){
document.getElementById('Div1').innerHTML='Info..';
document.getElementById('Div2').innerHTML='Info..';
document.getElementById('Div3').innerHTML='Info..';
}

i hope that this makes sense and i have made myself clear!!! thanks to anyone who can help me! im sure its something really easy but its just that i dont know! :) thanks

-dan
 
simple, use the eval function:
eval(agency+"()")

this will call the agencX() function...

Known is handfull, Unknown is worldfull
 

The eval method is good, but suffers from a latency issue. Consider something like this instead of eval:

Code:
window['Agency' + x]();

Haven't tried it, but if it works it will be slightly faster than the eval method.

Hope this helps,
Dan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top