Javascript Anchor Tips & Tricks

In this article i will try to explain how can we use javascript to jump to different anchor within a page.

Anchor HTML Code

[sourcecode language=”html”]

[/sourcecode]

Anchor Link

[sourcecode language=”html”]

div style=”display: none”>

[/sourcecode]

Example: Test HTML Code

Javascript function to jump to the anchor

[sourcecode language=”jscript”]

function goToAnchor(nameAnchor){
window.location.hash=nameAnchor;
}

[/sourcecode]

How to call javascript function to jump to anchor

[sourcecode language=”html”]Test Javascript Code
[/sourcecode]

Example: Test Javascript Code


Anchor is here –> <–



6 Comments

  1. Filo
    17th January 2009

    Very useful!! Thanks

    Reply
  2. niko
    17th January 2009

    i still don’t understand… but thanks i will try it again

    Reply
  3. nick
    17th January 2009

    Thanks for this – it was part of the answer to a problem that had stumped me for some time.

    Reply
  4. Terry
    17th January 2009

    How can I do this on a page with referrers?
    e.g. URL is admin.php?date=xxx&title=yyy …
    then I want it to jump to a named anchor

    ??

    Reply
  5. Sharl
    17th January 2009

    Great code! but it seems to that only work for tag,not support tags.

    Reply
  6. Phil
    17th January 2009

    I tried your code, and, sadly, it failed for me. I was never able to jump to the anchor tag; it constantly went to the top of the page even though I have used a href=”window.location.hash=’loc'”

    Reply

Leave A Comment

To Top