iphone上的pageshow事件只触发一次 [英] pageshow event on iphone fires only once

查看:1829
本文介绍了iphone上的pageshow事件只触发一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在safari(iphone)上使用 pageshow 事件修复后退按钮缓存的一些问题。但是在使用后退按钮时它似乎只工作一次。

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.

我在页面上有这个处理程序 A

I have this handler on page A:

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

然后我转到页面 B 然后去回到 A - 一切正常。但是当我再次转到页面 B 并再次回到 A 时,没有任何反应。

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.

示例:
转到这个小提琴: https:// jsfiddle。 net / y278q8q0 / ,然后导航到任何其他页面并使用后退和前进按钮进行播放。该事件只会触发一次。

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.

它与ios 8.4在iPhone 6上的外观如下:
https://vid.me/5WPe

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

编辑

问题被标记为此问题的副本:''页面显示' Safari上的按钮* IPad"

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

很难说这些问题是否有相同的原因。在我的情况下,事件总是在开始时触发一次。此外,我尝试从提到的问题中实现所有非jquery解决方案,但它们都不适合我。

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.

推荐答案

可能同样的问题已于2012年发布 a>。

Probably same problem was solved in post from 2012.

所以你的代码应该是:

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

我现在无法访问safari,所以我无法测试它。请回答这是否正确。

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

这篇关于iphone上的pageshow事件只触发一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆