Ignore Safari bootstrap start page

I use pushState and create a pageview on popstate events based on the history.state of the current page.

If there is no story. Status (hopefully) to reload the document.

window.onpopstate = function(event) { if( window.history.state !== null ){ // page was ajax created and history created by pushState // recreate with ajax } else{ // page was loaded from server normally document.location.reload() } } 

The problem is that Safari fires a popstate event when the page loads. Chrome and Firefox launch pop-ups on the back of the browser.

I want to ignore the start popstate event of loading on Safari (ideally without setTimeout and without browser detection).

In addition, the site is a mixture of links - some of which will launch pushState and some that will load normally from the server - so if the user has ten pages in the navigation history and clicking the "Back" button, there will be a mix of pushState and pages without pushState.

+7
javascript jquery safari
source share
1 answer

I solved the problem of saving the initial value of the order obtained from window.history.state.order in a variable. When the on popstate event is fired, the previously stored value is compared with the current order value. They are the same, then nothing needs to be done (you should ignore the initial popstate value caused by Safari). Here you can see an example:

 var initial_oder = window.history.state.order || 0; window.onpopstate = function(event) { if( window.history.state !== null && window.history.state.order !== initial_order){ // page was ajax created and history created by pushState // recreate with ajax } else{ // page was loaded from server normally document.location.reload() } } 

Hope this help!

+5
source share

All Articles