javascriptphpjquerygoogle-chromepage-jump

Jump to name=key-1 when URL contains #key-1


I wish to jump to the selected hash whenever my URL contains a #.

The # are dynamically created and inserted into header($location);. Afterwards the new URL loads like this: www.example.com?p=1#key-1, however it wont go to the selected # on the page. How can I do this?

I looked into javascript window.location.hash, however I couldn't figure out how to use it properly.

UPDATE: The # in the url works when I manually type #sek-31 into the adress bar. However, when I reload the same URL, I get thrown back to the top of the page.

UPDATE TWO: enter image description here

UPDATE THREE:

Its working in the following browsers:


Solution

  • If you go to www.example.com?p=1#key-1 the browser automatically will scroll to the element with id key-1. So, just add a id attribute.

    https://developer.mozilla.org/en/docs/Web/HTML/Element/a#attr-href

    (This url is already demo for this. It end with #attr-href when you click on it you will go directly to the section #attr-href)

    http://output.jsbin.com/bajiqu#res Notice that I put #res in the url so the browser will scroll to the #res paragraph.