AnKing
AnKing

Reputation: 2174

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?

Upvotes: 0

Views: 100

Answers (1)

thepratt
thepratt

Reputation: 323

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"

Upvotes: 1

Related Questions