【问题标题】:How to show dialog box if user wants to close the browser and then check what they selected?如果用户想要关闭浏览器然后检查他们选择的内容,如何显示对话框?
【发布时间】:2018-05-27 11:12:25
【问题描述】:

我想知道如果用户想关闭浏览器/窗口,JavaScript 或 JQuery 中是否有任何解决方案可以向用户显示消息?我知道有一个使用beforeunload 的选项,但唯一的问题是我找不到用户选择Leave PageStay on Page 的内容。我问这是否可能的原因是因为如果用户选择 Leave Page 我想处理 AJAX 调用然后关闭浏览器。到目前为止,所有解决方案都将在用户选择选项之前运行 AJAX 调用。这是我当前使用的函数的示例:

window.addEventListener("beforeunload", function (e) {
    var confirmationMessage = "Your browser is now offline.",
    recID = $.trim($("#recordID").val());

    recID ? removeLock(recID) : ""; // Call function to remove record from the Lock table.
    (e || window.event).returnValue = confirmationMessage; //I'm not sure what is the purpose of this line ???
    return confirmationMessage;
});

正如您在上面的代码中看到的那样,只要用户单击 X 关闭浏览器/窗口,我的 removeLock() 函数就会执行。这将从锁定表中删除记录。如果用户决定留在页面上,这可能是问题所在。我想知道是否有任何替代解决方案或检查用户选择的方法?谢谢。

【问题讨论】:

  • @freedomn-m 我已经阅读了那个答案。所以这意味着这种情况没有解决方案?
  • 您可以使用confirm() - 看起来不太好,但确实会阻止用户界面(因此不会关闭)。不过可能会惹恼您的用户...
  • @freedomn-m 你能提供一个例子来说明如何为关闭窗口/浏览器实现 confirm() 吗?

标签: javascript jquery onbeforeunload onunload


【解决方案1】:

我阅读了您的帖子,因为这可能有一天会派上用场跟踪用户行为。

我认为您的问题已在较早的帖子中得到解答: Capturing result of window.onbeforeunload confirmation dialog

HTH

【讨论】:

    猜你喜欢
    • 2020-03-07
    • 1970-01-01
    • 2012-09-23
    • 2016-10-05
    • 2015-07-21
    • 2014-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多