如何在“beforeunload”中调用函数然后关闭浏览器/窗口?

时间:2017-12-12 16:36:01

标签: javascript jquery onbeforeunload

如果用户点击“离开页面”,我想运行功能。该函数将发送一个AJAX调用。为了执行此功能,浏览器必须保持打开状态。所以我想知道如果完成/完成此功能后我怎么能关闭窗口?这是我的例子:

 window.addEventListener("beforeunload", function (e) {
       var confirmationMessage = "Your browser is now offline.",
       recID = $.trim($("#recID").val());
       removeUnlock(recID); // Call function to remove record from the Locked table.
       $("#unlockBtn").prop("disabled",true); //Set Unlock button attribute to disabled.
       $('.blockEdit').prop("disabled",true); //Set Edit button attribute to disabled. 

       (e || window.event).returnValue = confirmationMessage; //Gecko + IE
       console.log(confirmationMessage);
       return confirmationMessage; //Webkit, Safari, Chrome
});

我已经更新了我的代码示例。我找到了调用解锁功能的方法,但有一个问题。无法检查用户是否在页面上选择了“离开”或“停留”。解锁功能应该仅在用户选择离开页面时运行。在那种情况下,就像我提到的那样,没有足够的时间来处理ajax调用。我不确定标记为重复的问题可以解释或解决这种情况......

0 个答案:

没有答案
相关问题