我一直在尝试发送信标beforeunload
它似乎适用于几乎所有现代浏览器,除了隐身模式下的 Chrome。
这是适用于所有现代浏览器的代码,除了 Chrome 的隐身模式:
window.onbeforeunload = function() {
navigator.sendBeacon("url");
}
甚至这个代码似乎也不起作用:
window.onbeforeunload = function() {
console.log('before unload')
}
我做错了什么还是这只是 Chrome 的错?
您的设置是什么(SO,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 时发送的请求屏幕(隐身模式):
Furthermore, notice:
为了阻止不需要的弹出窗口,某些浏览器不显示提示
在 beforeunload 事件处理程序中创建,除非该页面已被
互动过。此外,有些根本不显示它们。
Ref: https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)