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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script not working in internet explorer

Status
Not open for further replies.

wilcosky

Technical User
Feb 18, 2007
4
I have this script on my page that makes my links in my menu become highlighted when clicked then when the next link is clicked the previous link goes back to normal and the new link because highlighted etc. anyway, it works in firefox and safari but not internet explorer... any know why?

<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;
if (link.addEventListener) {
link.addEventListener("click",rememberLink,false);
} else if (link.attachEvent) {
link.attachEvent("onclick",rememberLink);
} else {
link.onclick=rememberLink;
}
}
}
</script>


oh yeah, I do have the body onload tag in the beginning of the body in the html, just thought I'd mention that before someone said, well I know whats wrong, you have to people body onload blah blah in the body... I got that... and like I said it works in all browsers BUT internet explorer.

If noone knows whats wrong, then does anyone know of a script that will do what im talking about... cause I have a website where everything opens up in the same page using ajax. So I want a menu that will let the user know what section that are in.
 
>lastClickedLink = this;

It is "off-standard" for moz to recognize the way it does of "this" keyword here. But since it does, you can do this.
[tt]
var obj = (window.event)?window.event.srcElement:this;
lastClickedLink = obj;
obj.style.color = "white";
obj.style.backgroundColor = "green";
obj.style.fontStyle = "italic";
[/tt]
To do it more inline with the ecma, you can replace the first line with this.
[tt]
var obj = (window.event)?window.event.srcElement:arguments[arguments.length-1].target;
[/tt]
 
what did you mean by:
To do it more inline with the ecma, you can replace the first line with this.

var obj = (window.event)?window.event.srcElement:arguments[arguments.length-1].target;


cause the code works fine the first way you suggested... is it important to change the first line to that code above? and what is ecma?

The code works fine like I said by changing the "this" to "obj" so I don't think I'm going to mess with the code anymore, I mean, if it works, why fix it... so thank you very much for correcting my code!! :)

I am still just curious about what would be the difference if I replaced the first line with this:

var obj = (window.event)?window.event.srcElement:arguments[arguments.length-1].target;
 
If you're happy with the first version, go with it. Each browser has its own specificity which the designer thinks add value to the functionality. No, you don't need to change. Just that it would not be a very standard deduction to recognize the "this" keyword in this setting. So you've to memorize it for this specific case. The passing of event as the last argument to the function referenced this way is more universal. It is applicable to both ie as well as moz (ff/nn) for event handling. Only now, srcElement and target still differenciate them. That's life.
 
ok, I went ahead and changed the first line to what you suggested... it works in firefox and safari, but im currently on a mac without internet explorer... I was wondering if you or anyone happens to be online now with internet explorer as your browser... if so could you go to and see if the script works, cause at this time I have no way of testing it on internet explorer.

Just click the menu on the right and when you do, if it stays green and no error messages appear in the status bar, then its working...

Thank You!
 
Ok, I just asked a friend who has internet explorer if it works and he says yes, so nevermind on my last comment.

Everything works in all browsers that I've tried!! Even with the:
var obj = (window.event)?window.event.srcElement:arguments[arguments.length-1].target;

added.

Of course you probably already knew that it would work, but just in case you had any doubts... it works.
Everything works.

So, thank you again for the corrections to my script!!!!!!

Billy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top