Search code examples
javascriptwindowanchor

Go to anchor without changing url


On loading a page I would like it to go to #content without changing the url.

I thought I would be able to use

window.location.hash = "content";
window.location.replace("#content", "");

but #content still exists on the url.

Any better method?


Edit:

Also tried

window.location.hash = "content";
window.location.replace(window.location.toString().replace("#content", ""));

But this sends the browser into a loop.


Solution

  • You could find the vertical position of the anchor with that id, and then scroll to that position.