简体   繁体   English

离开页面时不触发jQuery beforeunload事件

[英]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.

相关问题 关闭(不离开)页面时的jQuery beforeunload? - jquery beforeunload when closing (not leaving) the page? 在 Safari 上刷新页面后,beforeunload 事件未触发 - beforeunload event not firing after page refresh on Safari jQuery beforeunload事件未在后退按钮上触发 - jQuery beforeunload event not firing on back button jQuery beforeunload事件未在任何浏览器中触发 - Jquery beforeunload event not firing in any browser jQuery beforeunload自定义弹出窗口,用于离开页面 - jQuery beforeunload custom pop up window for leaving a page 在 Sharepoint 表单中添加附件时,使用 addEventListener beforeunload 保存事件不会触发 - Save event with an addEventListener beforeunload not firing when add attachments in Sharepoint form Javascript/jQuery beforeunload 和 unload 不触发 - Javascript/jQuery beforeunload and unload not firing 在离开画布jQuery时touchleave没有触发 - touchleave not firing when leaving the canvas jQuery NextJs<Link> 不会触发 beforeunload 事件。 尝试在离开页面之前提示用户 - NextJs <Link> wont trigger beforeunload event. Trying to prompt user before leaving page “beforeunload”事件不会在谷歌浏览器上触发弹出窗口 - "beforeunload" event not firing on Google Chrome for popup window
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM