Page not reloading via 'back' when using pushState() / onpopstate

前端 未结 1 1044
孤独总比滥情好
孤独总比滥情好 2021-01-01 17:42

I am refreshing some pages with AJAX and so an updating the history with the following code -

/** Update the page history */
var pushState_object = {
    aja         


        
相关标签:
1条回答
  • 2021-01-01 18:33

    The initial state does not have a .state property available, because you didn't use .pushState to add such data (it was loaded the normal way as you describe).

    What you do know though, is that if there is no .state, it must be the original state, so you can use an else block like this: http://jsfiddle.net/pimvdb/CCgDn/1/.

    Lastly, you can use e.state.

    window.onpopstate = function(e){
        if(e.state !== null) { // state data available
            // load the page using state data
            initiate_load_updated_page(e.state.ajax_string, window.history.state.security, 0);
    
        } else { // no state data available
            // load initial page which was there at first page load
        }
    }
    
    0 讨论(0)
提交回复
热议问题