[英]Is there any way to kill session on closing browser? I have used the onunload and onbeforeunload event. but it doesnt work
I have used the (window.onunload) and (window.onbeforeunload) event for capturing browser close event to kill session. 我已经使用(window.onunload)和(window.onbeforeunload)事件来捕获浏览器关闭事件以终止会话。 but it doesn't work for me in chrome.
但是它不适用于Chrome。 Is there any way to do?
有什么办法吗?
Normal async XMLHttpRequest
(including fetch
) tend to be cancelled as the page closes. 正常的异步
XMLHttpRequest
(包括fetch
)往往会在页面关闭时被取消。 The usual workaround has been to make synchronous requests. 通常的解决方法是发出同步请求。
However, there's a new way to do it: navigator.sendBeacon
. 但是,有一种新方法可以实现:
navigator.sendBeacon
。 It was designed with pretty much that exact purpose in mind - it will queue a request to be executed even after the page is closed. 它的设计几乎出于精确的目的-即使在关闭页面后,它也会将要执行的请求排队。
window.addEventListener("unload", function() {
navigator.sendBeacon("/logout");
});
It still doesn't work on all browsers, so if you care about the lag-behinders you might want to test if it is available, and use the above-mentioned workaround if not. 它仍然不能在所有浏览器上运行,因此,如果您在乎滞后,您可能需要测试一下是否可用,如果没有,请使用上述解决方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.