[英]JQuery beforeunload event not firing when leaving the page
I'd like to send an AJAX request when a user leaves my site in order to kill their session. 我想在用户离开我的网站时发送AJAX请求以终止其会话。 I've done this:
我已经做到了:
$(document).ready(function() {
$(window).bind('beforeunload', function () {
$.get("/forceLogout");
});
});
and it works perfectly if I close the tab. 如果我关闭标签页,它会完美地工作。 However if I just type google.com in the address bar and press enter, the event does not fire.
但是,如果我只是在地址栏中输入google.com,然后按Enter,则不会触发该事件。 What am I doing wrong here?
我在这里做错了什么?
You can't make an asynchronous call at the time the page is being unloaded. 您无法在页面卸载时进行异步调用。 Try this
尝试这个
$(document).ready(function() {
$(window).bind('beforeunload', function () {
$.ajax({
type: "GET",
url: "/forceLogout",
async: false
});
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.