iOS Safari Mobile doesn't trigger pageshow firing only once

前端 未结 3 462
离开以前
离开以前 2021-01-14 07:09

The iOS Safari doesnt\'t seem to trigger pageshow event in the following situation.

Lets say I have 3 pages

  • Page A : (has some code code on pageshow ev
相关标签:
3条回答
  • 2021-01-14 07:44

    Hack : This is what worked for me

    var myCustomEvent = (navigator.userAgent.match('iPhone') != null) ? 'popstate' : 'pageshow';
    
    $(window).on(myCustomEvent, function(e) {
     ...
    }
    

    For some reason popstate triggers everytime when page state changes in iOS but not in Android.

    0 讨论(0)
  • 2021-01-14 07:50

    The popstate event doesn't seem to work any more, at least for me. I worked out some third-party script on my page was breaking this, but wasn't able to work out which one. I came up with this hack:

    addEventListener('pageshow', () => {
      history.replaceState({}, document.title, window.location.pathname);
      // called on initial load and first back
    });
    
    addEventListener('popstate', () => {
      // called on all back events
    });
    
    0 讨论(0)
  • 2021-01-14 07:53

    Try using:

    window.onpageshow = function(event) {
    if (!event.persisted) {
        alert("hello");
    }
    };
    

    Persisted is false on initial page load, so you can check against it, and if it false, it is your first page load.

    0 讨论(0)
提交回复
热议问题