Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

history pushState and scroll position

I am trying to retrieve the scroll position when a user navigates back in the browser history using HTML5 popstate handler.

Here is what I have:

$(document).ready(function () {

    $(window).on('popstate', PopStateHandler);

    $('#link').click(function (e) {

        var stateData = {
            path: window.location.href,
            scrollTop: $(window).scrollTop()
        };
        window.history.pushState(stateData, 'title', 'page2.html');
        e.preventDefault();

    });

});

function PopStateHandler(e) {
    alert('pop state fired');
    var stateData = e.originalEvent.state;
    if (stateData) {
        //Get values:
        alert('path: ' + stateData.path);
        alert('scrollTop: ' + stateData.scrollTop);
    }
}


<a id="link" href="page2.html"></a>

When I navigate back, I am unable to retrieve the values of the stateData.

I presume this is because the popstate is retrieving the values of the initial page load and not the state I pushed to the history when the hyperlink was clicked.

How could I go about getting the scroll position on navigating back?

like image 850
George Filippakos Avatar asked May 24 '13 09:05

George Filippakos


People also ask

What is history pushState?

In an HTML document, the history. pushState() method adds an entry to the browser's session history stack.

What is the difference between pushState and replaceState?

The big difference is, that while pushState will create a new entry in the browser's history, replaceState will only replace the current state. As a side effect of this, using the replaceState method will change the URL in the address bar, without creating a new history entry.

What is history scrollRestoration?

scrollRestoration. The scrollRestoration property of History interface allows web applications to explicitly set default scroll restoration behavior on history navigation.

Does history pushState reload page?

The history. pushState() method can be used to push a new entry into the browser's history—and as a result, update the displayed URL—without refreshing the page. It accepts three arguments: state , an object with some details about the URL or entry in the browser's history.


1 Answers

I managed to solve this one myself:

We must overwrite the current page on the history stack before navigating to the new page.

This allows us to store the scroll position and then pop it off the stack when we navigate back:

    $('#link').click(function (e) {

        //overwrite current entry in history to store the scroll position:
        stateData = {
            path: window.location.href,
            scrollTop: $(window).scrollTop()
        };
        window.history.replaceState(stateData, 'title', 'page2.html');

        //load new page:
        stateData = {
            path: window.location.href,
            scrollTop: 0
        };
        window.history.pushState(stateData, 'title', 'page2.html');
        e.preventDefault();

    });
like image 55
George Filippakos Avatar answered Sep 23 '22 09:09

George Filippakos