javascriptpushstatepopstate

Javascript pushState, onpopstate - after ~10 states - clicking 5th step (on back button) and it outputs 4th step


Code using to pushState

        window.history.pushState({"html":r[0],"pageTitle":r[1], 'bread_crumbs': r[2], 'page': r[3], 'parentid': r[4], 'pageid': r[5]},"", h);

Code using to check states

window.onpopstate = function(e) {
    console.log(e.state.page+'|'+location.href);

So let's say I visited 10 pages - when I click page #5, it is displaying good title in browser. But for some strange reason it ouputs page #4.

It works if I click back button, may be you know - Chrome displays all steps once click second mouse button. So here happens that issue.

Thank you.

You can try it here http://demo.ajax-cart.com/

Click 1. Test category 2. Brands 3. Blog 4. About 5. News

Go with back button to "Our Blog" - you will see "Brands".


Solution

  • I found solution

    It was

                document.title = r[1];
                window.history.pushState({"html":r[0],"pageTitle":r[1], 'bread_crumbs': r[2], 'page': r[3], 'parentid': r[4], 'pageid': r[5]},"", h);
    

    But it must be

            window.history.pushState({"html":r[0],"pageTitle":r[1], 'bread_crumbs': r[2], 'page': r[3], 'parentid': r[4], 'pageid': r[5]},"", h);
            document.title = r[1];