【问题标题】:window:beforeunload on IE 11 - always pop up the unsave dialogwindow:beforeunload on IE 11 - 总是弹出取消保存对话框
【发布时间】:2018-12-10 10:49:45
【问题描述】:

为 beforeunload 事件向全局窗口对象添加侦听器时,IE 11 的行为与 Chrome 和 Firefox 不同。

这与Angular“ngForm”一起使用,当表单脏且未提交时,用户尝试点击另一个组件(路由),它会弹出警告对话框。如果用户不修改表单,则不会弹出浏览器原生对话框提示。

但是,在 IE 中,即使用户没有修改表单,然后尝试导航到其他组件,它仍然会弹出警告对话框。

我不确定是不是因为 IE 中的 window: beforeunload 每次导航到其他组件时都会卸载页面?

  @HostListener('window:beforeunload')
  canDeactivate(): boolean {
    return !this.form.dirty || this.form.submitted;
  }

【问题讨论】:

    标签: javascript angular forms cross-browser internet-explorer-11


    【解决方案1】:

    一个几乎跨浏览器的工作示例将接近以下内容:

    window.addEventListener("beforeunload", function (e) {
      var confirmationMessage = "\o/"; // is equivalent to event.preventDefault();
    
      e.returnValue = confirmationMessage;     // Gecko, Trident, Chrome 34+
      return confirmationMessage;              // Gecko, WebKit, Chrome <34
    });
    

    所以在返回之前,请确保您也更新了event.returnValue

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多