Search code examples
javascriptphphtmlbrowserpushstate

Javascript window.history.pushState is not detecting the browser refresh button


I am trying to refresh window.history.pushState url which refreshes the page with F5 or CTRL+5 but when i press the refresh button of browser it gives error.

F5/CTRL+F5 or even browser refresh button will refresh the real url like http://www.dddd.com/myrealpath/test/ but after history.pushState the F5/CTRL+F5 refresh the http://www.dddd.com/myrealpath/test/1/done/ with the state trick.

But browser refresh button does not refresh http://www.dddd.com/myrealpath/test/1/done/, it gives error of Object not found!.

Kind help is needed. Thanks

path = window.location.pathname;
  
window.history.pushState(null, null, path+"1/done/");

if (performance.navigation.type == 1) {
    window.history.pushState(null, null, path+"1/done/");
}

$(document).on('keydown', function(event) {
    
    if (event.ctrlKey && (window.event.keyCode == 116)) {
        window.history.pushState(null, null, path);
    }
    if (window.event.keyCode == 116) {
        window.history.pushState(null, null, path);
    }
    
    if (event.ctrlKey && (event.keyCode == 123 || event.keyCode == 117)) { 
        window.history.pushState(null, null, path);
    }

});

Solution

  • I found it by doing the below now browser refresh button is working also.

    //master path - original path
    path = window.location.pathname;
    
    //Redirectss original path to this
    window.history.pushState(null, null, path+"1/done/");
    
    jQuery(window).bind("unload", function() { //
        window.location.href = path; // original path
    });