gpt4 book ai didi

javascript - onbeforeunload 根本不起作用

转载 作者:行者123 更新时间:2023-12-02 14:33:58 28 4
gpt4 key购买 nike

我有这个代码:

(function() {
window.onbeforeunload = function () {
alert("hi")
console.log("unloading");
};
})();

我也有这个代码:

window.addEventListener("beforeunload", function() {
alert("hi")
console.log("unloading")
});

它们似乎都不起作用。我想做的就是在用户尝试离开页面时进行记录,但这并没有发生(最新的 Chrome、最新的 Firefox、Edge...),任何人都可以解释为什么它不起作用吗?

最佳答案

Since 25 May 2011, the HTML5 specification states that calls to window.alert(), window.confirm(), and window.prompt() methods may be ignored during this event. See the HTML5 specification for more details. Note also that various mobile browsers ignore the result of the event (that is, they do not ask the user for confirmation). Firefox has a hidden preference in about:config to do the same. In essence this means the user always confirms that the document may be unloaded.

Source

此外,在离开之前使用return语句提示用户

window.addEventListener("beforeunload", function() {
return "hi";
});

关于javascript - onbeforeunload 根本不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37613401/

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