【问题标题】:How to ask a person if they want to close a window? [duplicate]如何问一个人是否想关窗? [复制]
【发布时间】:2012-10-11 23:23:18
【问题描述】:

可能重复:
Javascript To Get An Alert When Closing The Browser Window

我有正当理由问这个问题,我有一个管理大型数据库的 Web 应用程序,并且用于修改查找表的页面会在单独的窗口中弹出。我希望能够提醒用户他们对表单所做的更改尚未保存到数据库中,并为他们提供这样做的选项,即使他们单击“X”关闭窗口也是如此。这可能吗?

【问题讨论】:

标签: php javascript html popup


【解决方案1】:

这就是我的工作

window.onbeforeunload = function (e) {
  var e = e || window.event;
  if (allowNavigationsAwayFromPage != true)
      {
      // For IE and Firefox
      if (e) {
        e.returnValue = 'Are You Sure? Some operation is still going on';
      }

      // For Safari
      return 'Are You Sure? Some operation is still going on';

      }
};

allowNavigationsAwayFromPage 现在是一个变量。但我通常有一个功能来检查这个。

【讨论】:

    【解决方案2】:

    最简单的方法是:

    window.onbeforeunload = function(e)
    {
        e = e || window.event;
        if (confirm('Are you sure?\nNot everything has been saved yet'))
        {
            return e;//return event object, unaltered... page will unload
        }
        if (e.preventDefault)
        {
            e.preventDefault();
            e.stopPropagation();
        }
        else
        {
            e.returnValue = false;
            e.cancelBubble = true;
        }
        return false;
    };
    

    【讨论】:

      猜你喜欢
      • 2016-06-02
      • 1970-01-01
      • 1970-01-01
      • 2018-07-30
      • 2020-01-27
      • 1970-01-01
      • 1970-01-01
      • 2016-09-22
      • 2013-06-04
      相关资源
      最近更新 更多