pageshow event on iphone fires only once

2019-03-26 07:57发布

问题:

I'm trying to use pageshow event on safari (iphone) to fix some problems with back button cache. But it seems to work only one time when using back button.

I have this handler on page A:

window.addEventListener("pageshow", function () {
    alert("pageshow");
});

Then i go to page B and go back to A - everything works fine. But when I go to page B again and go back to A again, then nothing happens.

example: go to this fiddle: https://jsfiddle.net/y278q8q0/, then navigate to any other page and play with back and forward buttons. The event will fire only once.

That how it looks on iphone 6 with ios 8.4: https://vid.me/5WPe

edit:

question was marked as a duplicate of this one: 'pageshow' is not received when pressing "back" button on Safari on *IPad"

It's hard to say if those problems have the same cause. In my case event always fires once at the beginning. Also I tried to implement all non-jquery solutions from mentioned question and none of them is working for me.

回答1:

Probably same problem was solved in post from 2012.

So your code should be:

window.addEventListener("pageshow", function () {
   if (event.persisted) {
     alert("pageshow");
   }
});

I don't have access to safari right now, so I can't test it.Please answer whether this is correct.