首页 > 解决方案 > 从 PerformanceNavigationTiming 确定导航类型

问题描述

我曾经通过单击上一页中的后退按钮来确定用户是否来过,使用window.performance.navigation.typelike

if (window.performance.navigation.type === 2) {
    window.location.reload()
}

我看到此属性已被Navigation Timing Level 2弃用并成功。我怎样才能模仿 performance.navigation api 的行为?

标签: javascripthtmlbrowser

解决方案


我应该先说我不是 JavaScript 专业人士。这是我利用Navigation Timing Level 2的方法。

if (String(window.performance.getEntriesByType("navigation")[0].type) === "back_forward") {
    window.location.reload()
}

window.performance.getEntriesByType("navigation")返回每个标记集的 PerformanceEntry 对象列表。如果您没有设置任何标记,则此列表包含一个具有窗口导航信息的对象。


推荐阅读