Friday, 18 September 2020

How to maintain browser history while doing a location.reload() on popstate event of Javascript

So, I have a scenario where I am generating all my URL's using history.pushState function. I am NOT using any server side technique for this. I am fetching all the data only once from the server and then generating the URL using Javascript.

Now the problem that I am encountering is using the popstate function which basically mimics the back button behavior of a browser.

Everything is working fine for ONLY one back button click.

    $(window).on('popstate', function (event) {
        location.reload(true);
        //window.location.href = document.location;
        //console.log("location: " + document.location);
    });

After one back button event, I am unable to see any previous history of the browser. I am assuming it is happening because of location.reload method. I tried storing all my location also and then doing a redirect but same thing, the browser history gets lost after one refresh.

Is there a way where I can achieve this without the browser losing it's history for multiple back button clicks?

If there is a JQuery option for this, then it would be great if someone could share their knowledge on this aspect.



from How to maintain browser history while doing a location.reload() on popstate event of Javascript

No comments:

Post a Comment