Safari上的pageshow活动 [英] pageshow event on Safari

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

问题描述

我有以下简单的JavaScript,我需要在页面重新加载时重新初始化一些变量,特别是当页面从Safari BFCache提供时:

I have the following simple JavaScript, which I require in order to re-initialise some variables on page reload, specifically when the page is served from the Safari BFCache:

jQuery(window).bind('pageshow', function(event) {
    // I could inspect event.originalEvent.persisted here to check for BFCache hits,
    // but it isn't required in this instance.

    // Do some initialisation here
});

假设用户遵循以下路径,导航到B和C页,浏览(即使用每个后面的后退按钮)到页面A:

Let's say the user follows the below path, navigating to pages B and C, browsing back (i.e. using the back button) to page A after each:

A - > B - > A - > C - > A

A -> B -> A -> C -> A

pageshow 处理程序仅在第一次和第二次访问A时触发,即初始页面加载和第一次BFCache加载。因此,从本质上讲,似乎从BFCache提供的页面 pageshow 只提供一次。

The pageshow handler only fires for the first and second visits to A, i.e. the initial page load and the first BFCache load. So in essence, it seems pages served from the BFCache fire pageshow once and only once.

这是预期的吗?在Safari上的行为,有没有办法呢?

Is this the expected behaviour on Safari, and is there a way around it?

推荐答案

我似乎已经解决了这个问题,而是绑定到 popstate 事件:

I appeared to have solved the issue by instead binding to the popstate event:

jQuery(window).bind('popstate', function(event) {
    // Do some initialisation here
});

只要活动历史记录条目发生变化,就会触发。这似乎始终如一,而不仅仅是一次,因此无论用户何时登陆,都可以实现重新初始化的目标。

which fires whenever the active history entry changes. This appears to fire consistently rather than just once, so achieves the goal of performing re-initialisation whenever a user lands on page, regardless of the mechanism for getting there.

这篇关于Safari上的pageshow活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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