简体   繁体   中英

html anchor links

I want there to be anchor links on a webpage where when I click on the link, it brings me to a certain part of the webpage.

However, when the user click the back page button, it is now bringing the user to the previous part of the page, instead of the previous page.

How should I implement the link so that it does bring the user to the previous page instead of the previous part of a page?

<a href="#some part">link</a>

I wonder if you could do something like what is described in the top answer of this post . Put the following into the 'onclick' of your anchor, and it'll scroll to your div of choice, without changing the URL (and hence preserving back-button functionality).

document.getElementById('youridhere').scrollIntoView(); 

Don't forget to take out the "href=#somepart".

That is how the anchor tags work. Your best bet is to read here and use it to remove your anchor jumps from browser history.

https://developer.mozilla.org/en/DOM/Manipulating_the_browser_history

What you are describing is expected behavior, but if you really want to change it I'd try doing this:

capture the onbeforeunload event, and then use the history.go function to jump back to a url that doesn't have a hash in it.

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