473,386 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

lastLinkClicked

Hello,

I found an old post similar to the question I was looking for but I still need answers. I have a website where all or most of the pages open up in the same page using ajax, kinda like an iframe. Yeah, well, I want the links in the menu to change to a different color when clicked and then stay that color until a new link is clicked... then once a new link is clicked the other link goes back to normal and the new link that was clicked changes to a different color and stays that way until another link is clicked and so on.

The script I'm using is the following which is in the <head> of my html:

<script type="text/javascript">
var lastClickedLink = null;
function rememberLink() {
if (lastClickedLink) {lastClickedLink.style.color = "green";lastClickedLink.style.backgroundColor = "white";lastClickedLink.style.fontStyle = "normal";}
lastClickedLink = this;
this.style.color = "white";
this.style.backgroundColor = "green";
this.style.fontStyle = "italic";
}
function makeMemory() {
for (var i=0; i<document.links.length; i++) {
var link = document.links[i];
if (link.addEventListener) {
link.addEventListener("click",rememberLink,false);
} else if (link.attachEvent) {
link.attachEvent("onclick",rememberLink);
} else {
link.onclick=rememberLink;
}
}
}
</script>

Then in the body I have:
<body onload="makeMemory()">

Now it works PERFECTLY in Firefox and even Safari, but, not in Internet Explorer. Does anyone know why this doesn't work in internet explorer?

OR does anyone know of a better code to make this work... I just want people to be able to tell what section they are in on my site, which is obvious, but, I like that effect of the link changing and staying until they go to another page...
Feb 17 '07 #1
3 1228
anyone? anyone at all?
Feb 19 '07 #2
r035198x
13,262 8TB
Hello,

I found an old post similar to the question I was looking for but I still need answers. I have a website where all or most of the pages open up in the same page using ajax, kinda like an iframe. Yeah, well, I want the links in the menu to change to a different color when clicked and then stay that color until a new link is clicked... then once a new link is clicked the other link goes back to normal and the new link that was clicked changes to a different color and stays that way until another link is clicked and so on.

The script I'm using is the following which is in the <head> of my html:

<script type="text/javascript">
var lastClickedLink = null;
function rememberLink() {
if (lastClickedLink) {lastClickedLink.style.color = "green";lastClickedLink.style.backgroundColor = "white";lastClickedLink.style.fontStyle = "normal";}
lastClickedLink = this;
this.style.color = "white";
this.style.backgroundColor = "green";
this.style.fontStyle = "italic";
}
function makeMemory() {
for (var i=0; i<document.links.length; i++) {
var link = document.links[i];
if (link.addEventListener) {
link.addEventListener("click",rememberLink,false);
} else if (link.attachEvent) {
link.attachEvent("onclick",rememberLink);
} else {
link.onclick=rememberLink;
}
}
}
</script>

Then in the body I have:
<body onload="makeMemory()">

Now it works PERFECTLY in Firefox and even Safari, but, not in Internet Explorer. Does anyone know why this doesn't work in internet explorer?

OR does anyone know of a better code to make this work... I just want people to be able to tell what section they are in on my site, which is obvious, but, I like that effect of the link changing and staying until they go to another page...
Better luck in the Javascript forum for this.
Feb 19 '07 #3
acoder
16,027 Expert Mod 8TB
Unfortunately, IE does not support the this keyword properly - it doesn't refer to the element that the event occurred on. See this page (at the bottom).
Feb 19 '07 #4

Sign in to post your reply or Sign up for a free account.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.