...

View Full Version : Problem with globals variables



redd
05-26-2005, 10:03 PM
Hi,
First of all, Let me explain what I want to do:

I built a helpdest for the support in our entreprise and I've been asked to do it so when they click on a link it Hightlight it. Highlghting the link was easy, but the the previously hightlighted object were still highlighted. So I figured that if I could copy the link object in a global variable I could modify it afterwar. howeve it's not working for some reason

There's the fuction im using:



<script language="JavaScript">
<!--
var previousLink;
function selectLink(lnk) {
lnk.style.fontWeight='bolder';
lnk.style.textDecoration='underline';
previousLink.style.textDecoration='none';
previousLink = lnk;
}
//-->
</script>


And there's the links im Generate with PHP


<a target="bugdetails" href="/foo/bar.php?id=6969" onclick="selectLink(this)">fubar help</a>


I was thinking of passing lnk by referennce but it seem it's doesnt work.

I hope I have ennough details and that my question isn't too noobish.

Thank a lot

Harry Armadillo
05-27-2005, 08:03 AM
The problem is that the first time you click a link, previousLink is undefined. Therefore previousLink.style.textDecoration='none'; results in an error and the function terminates. And since the function terminates before the line previousLink = lnk, previousLink will never get a valid assignment.

The solution is simple, test previousLink.
<script type='text/javascript'>
var previousLink;
function selectLink(lnk) {
lnk.style.fontWeight='bolder';
lnk.style.textDecoration='underline';
if(previousLink){
previousLink.style.textDecoration='none';
previousLink.style.fontWeight='normal';
}
previousLink = lnk;
}
</script>

redd
05-27-2005, 03:18 PM
Wow it work like wonder, I've been looking around for soo long to find what was wrong. For some reason the Javascript console wouldnt report anyting.

Thank a lot the support guys will be very happy :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum