gpt4 book ai didi

javascript - 使用脚本在浏览器中返回导航的事件

转载 作者:行者123 更新时间:2023-11-29 21:44:17 27 4
gpt4 key购买 nike

我需要知道用户是否点击了浏览器中的后退导航箭头。我使用了以下事件,但在单击后退导航箭头时没有发生此事件。

$(window).on("navigate", function (event, data) {

});

如果你知道,请提出你的答案。

最佳答案

您可能会使用历史的 popstate。

The popstate event is only triggered by doing a browser action such as clicking on the back button (or calling history.back() in JavaScript). And the event is only triggered when the user navigates between two history entries for the same document.

您必须向历史添加一个具有相同标题且不更改 url 的新条目

pushState(state, title, url)

当您拦截 onpopstate 时,您将执行所需的操作,解除事件绑定(bind),然后使用 history.back() api。

window.onpopstate = function(event) {
alert("location: " + document.location + ", state: " + JSON.stringify(event.state));
window.onpopstate = null;
history.back();
};

history.pushState({}, document.title, "");

这不是一个行之有效的方法,我只在 Chrome 中测试过。

关于javascript - 使用脚本在浏览器中返回导航的事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31745008/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com