0

I have a page foo.html with a lot of named anchors. The user jumps from #anchor1 to #anchor2 etc. But when the user reloads the page I want only foo.html to load and not foo.html#anchorX. Also when the user hits the back button, they should be taken to the page they were on before foo.html and not back to the last anchor. Can this be done?

location.replace() takes care of the back button. But it does not solve the reload problem.

desertnaut
  • 57,590
  • 26
  • 140
  • 166

2 Answers2

1

Just put a script at the beginning of the page that deletes the hash. That way each time they refresh it just gives you foo.html.

e.g.

<head>
     <script>
       document.location.hash='';
     </script>
</head>
desertnaut
  • 57,590
  • 26
  • 140
  • 166
Joe Thomas
  • 5,807
  • 6
  • 25
  • 36
-1

It is very easy:

if(location.hash)history.back();
Willie Cheng
  • 7,679
  • 13
  • 55
  • 68
  • How does that jump to an anchor without changing the history? This code goes back one step in the history... – BDL Sep 16 '22 at 08:43