How to set the focus to an anchor already the first time

S

Stefan Mueller

With the following code I try to set the focus to the first anchor just
after the page has been loaded. However, it doesn't work. Exactly spoken the
focus is set to the first anchor (if you press Enter, page1.html will be
loaded) but the caret (box around the anchor) is not visible.

I've the same behavior if I drag and drop the code to the browser window.
Is there something like a refresh?

Stefan

PS: With Mozialla and Opera it usually works, but not with Internet Explorer

=============================================

<html>
<head>
<script type = "text/javascript">
function formloaded() {
document.getElementById("MyLink1").focus();
}
</script>
</head>

<body onLoad = "formloaded()">
<a href = "page1.html" id = "MyLink1" tabindex = "1">Page 1</a>
<br>
<a href = "page2.html" id = "MyLink2" tabindex = "2">Page 2</a>
</body>
</html>

=============================================
 
R

RobG

Stefan said:
With the following code I try to set the focus to the first anchor just
after the page has been loaded. However, it doesn't work. Exactly spoken the
focus is set to the first anchor (if you press Enter, page1.html will be
loaded) but the caret (box around the anchor) is not visible.

This works for me (it uses the links collection):

<body onload="
if (document.links && document.links[0] && document.links[0].focus)
document.links[0].focus();
">

[...]
 
S

Stefan Mueller

This works for me (it uses the links collection):
<body onload="
if (document.links && document.links[0] && document.links[0].focus)
document.links[0].focus();
">

Unfortunately it also doesn't work for my Internet Explorer (version
6.0.2800.1106). I open the Internet Explorer and drag and drop the following
code to the browser window:

<html>
<body onload="if (document.links && document.links[0] &&
document.links[0].focus) {document.links[0].focus();}">
<a href = "page1.html" id = "MyLink1" tabindex = "1">Page 1</a>
<br>
<a href = "page2.html" id = "MyLink2" tabindex = "2">Page 2</a>
</body>
</html>

The link 'Page 1' has the focus (press Enter and page 1 gets loaded) but the
box around the anchor is not visible.
Stefan
 
R

RobG

Stefan said:
This works for me (it uses the links collection):

<body onload="
if (document.links && document.links[0] && document.links[0].focus)
document.links[0].focus();
">


Unfortunately it also doesn't work for my Internet Explorer (version
6.0.2800.1106). I open the Internet Explorer and drag and drop the following
code to the browser window:

<html>
<body onload="if (document.links && document.links[0] &&
document.links[0].focus) {document.links[0].focus();}">
<a href = "page1.html" id = "MyLink1" tabindex = "1">Page 1</a>
<br>
<a href = "page2.html" id = "MyLink2" tabindex = "2">Page 2</a>
</body>
</html>

The link 'Page 1' has the focus (press Enter and page 1 gets loaded) but the
box around the anchor is not visible.
Stefan

I have exactly the same version of IE and I get the box. Cest la vie.
 
S

Stefan Mueller

The link 'Page 1' has the focus (press Enter and page 1 gets loaded) but
I have exactly the same version of IE and I get the box. Cest la vie.

Hmmm, that's very strange. I've two computers and I have this problem on
both. It seems that I have to live with it and now I know that not everyone
has this problem.

Thanks for testing
Stefan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,573
Members
45,046
Latest member
Gavizuho

Latest Threads

Top