PDA

View Full Version : making a link active on page load



mcpalmer
03-29-2005, 09:50 PM
Hi. Can anyone tell me how to make a link active on page loading? I tried this:

document.getElementById("pic1").style.color="#FF0000";

but that serves only to make the link permanently active even when others are clicked. I really want the link state to be made active rather than emulate by changing the color of it. But i know not how to do it. Can anyone help?

jscheuer1
04-02-2005, 09:08 AM
Well in IE it is easy. Just give your link a unique id like so:
<a id="act" href="some.html">Some Link</a>and in your body tag do this:
<body onload="document.getElementById('act').focus()">but, it gets a little more complicated in NS and FF. Modify your link tag to look like this:
<a id="act" href="some.html" onfocus="this.style.color='red'" onblur="this.style.color=''">Some Link</a>and use the same body onload thing. This will work OK in IE as well.

mcpalmer
04-02-2005, 02:19 PM
Thanks, that worked a treat

jscheuer1
04-02-2005, 04:59 PM
You're welcome. For a little more backward compatibility and graceful degrading on unsupportive browsers try:
<html>
<head>
<title>Activate Link Onload Demo</title>
<script type="text/JavaScript">
function activate(link){
if(document.getElementById)
document.getElementById(link).focus();
else if (document.all)
document.all(link).focus();
}
</script>
</head>
<body onload="activate('act');">
<a href="some.html">Some Link</a><br>
<a href="another.htm">Another Link</a><br>
<a href="important.htm" id="act" onfocus="this.style.color='red'" onblur="this.style.color=''";>Active Onload Link</a>
</body>
</html>