在以下情况下,iOS Safari似乎无法触发pageshow事件。
假设我有3页
用户从A -> B
导航。按下后退按钮。 (Pageshow触发罚款)
用户然后导航到另一个页面可以是页面B或页面C.然后再次按下后退按钮。 (页面显示不会触发)
相反,如果用户再次最小化并最大化窗口或切换到另一个窗口并返回(通过按iPhone上的中间按钮),则再次触发pageshow事件。
Android上似乎一切正常
window.onpageshow = function(e) {
alert('hello');
}
还有其他人面对吗?我在这件事上花了好几个小时,想不出办法。
非常感谢任何帮助。
答案 0 :(得分:2)
哈克:这对我有用
var myCustomEvent = (navigator.userAgent.match('iPhone') != null) ? 'popstate' : 'pageshow';
$(window).on(myCustomEvent, function(e) {
...
}
出于某种原因{@ 1}}每次在iOS中更改页面状态时都会触发,而不是在Android中。
答案 1 :(得分:0)
尝试使用:
window.onpageshow = function(event) {
if (!event.persisted) {
alert("hello");
}
};
在初始页面加载时保持为false,因此您可以检查它,如果为false,则表示它是您的第一页加载。
答案 2 :(得分:0)
popstate
事件似乎不再起作用,至少对我而言。我在我的页面上制定了一些第三方脚本,但却无法解决这个问题。我想出了这个黑客:
addEventListener('pageshow', () => {
history.replaceState({}, document.title, window.location.pathname);
// called on initial load and first back
});
addEventListener('popstate', () => {
// called on all back events
});