简体   繁体   中英

Jump to named anchor without changing the history

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?

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

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.

eg

<head>
     <script>
       document.location.hash='';
     </script>
</head>

Does that solve your problem?

It is very easy:

if(location.hash)history.back();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM