Jump to content

Focus on anchor using Javascript


deadimp

Recommended Posts

It's a basic question, but: What's the best way to have javascript focus on a link without adding the identifier 'clause' to the end of an url?

Example:

index.html

Random html...
<a id='anchor' href='...'>Anchor</a>
More random html

To have a browser focus on the anchor via the url, you just add #anchor to your url.

 

This is what I've come up with:

<script language='javascript'>
function anchor_focus(name) {
var obj=document.getElementById(name);
obj.focus();
obj.blur();
}
//Example
anchor_focus("anchor");
</script>

That scrolls it into view, and of course deselects it to mimick appending "#anchor" to the url.

It's worked on Firefox, IE, and Opera (all Windows).

 

Any other suggestions?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.