简体   繁体   中英

make jQuery scrollTop Not to leave browser history

I'm looking for a solution that will make scrollTop not to leave records in browser history.

When i first launch the page I wanted browser to go to a specific section of it if url has a parameter with an internal anchor name. so i do this:

$('html,body').animate(
{
    scrollTop: $(".section[tagname='"+url_tagname+"']").offset().top
},
'slow');

But now when i hit browser "back" button it goes to the page top first. SO i actually have to hit "back" twice to go to previous page.

Is there a way to make scrolltop not to leave navigation history?

You don't want the page to save the #anchor link when clicked on the same page correct?

Use a listener, and prevent default link action.

$('a').on('click', function(event) {
    event.preventDefault();
    event.stopPropagation();
});

This will save you having multiples of the same page in history, but you'll still have the #anchor tag in the URL if navigated from another page.

Edit: Actual answer was provided by myself in comments: "You want to rewrite the URL? Your question was more towards anchor tags. If you want to change the url use: window.history.replaceState(). https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Manipulating_the_browser_history "

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