gpt4 book ai didi

javascript - 在 Chrome 隐身模式中未触发 onbeforeunload 事件

转载 作者:行者123 更新时间:2023-12-01 15:28:10 26 4
gpt4 key购买 nike

我一直在尝试在 beforeunload 上发送信标它似乎适用于几乎所有现代浏览器,除了处于隐身模式的 Chrome。

这是适用于所有现代浏览器的代码,除了处于隐身模式的 Chrome:

window.onbeforeunload = function() {
navigator.sendBeacon("url");
}

甚至这段代码似乎也不起作用:
window.onbeforeunload = function() { 
console.log('before unload')
}

我做错了什么还是只是 Chrome 的错?

最佳答案

你的设置是什么(所以,Chrome 版本)?

在 Chrome 80.0.3987.116/Ubuntu 64 和 Chrome 79.0.3945.130/Windows 10 上,以下代码段可以正常工作:

window.addEventListener('beforeunload', (event) => {
console.log("BEFORE")
navigator.sendBeacon("http://www.google.it");
// Cancel the event as stated by the standard.
event.preventDefault();
// Chrome requires returnValue to be set.
event.returnValue = '';
});

beforeunload 发送的请求屏幕(隐身模式):

enter image description here
此外,请注意:

To combat unwanted pop-ups, some browsers don't display prompts created in beforeunload event handlers unless the page has been interacted with. Moreover, some don't display them at all.



引用: https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload

关于javascript - 在 Chrome 隐身模式中未触发 onbeforeunload 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60217338/

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