【问题标题】:window return value when closing modal with the X button使用 X 按钮关闭模式时的窗口返回值
【发布时间】:2020-06-26 10:21:01
【问题描述】:

我正在开发一个 Web 应用程序,我们的客户使用以下方式调用它:

var result = window.showModalDialog('http://example.com/myapp','My webapp', 'dialogtop=0;dialogleft=0;dialogwidth:' + (window.screen.width - 200) + ';dialogheight:' + (window.screen.height - 200) + ';status=no;resizable=yes;scroll=yes;maximize:yes;minimize:no;' );

当他们关闭这个模态对话框时,他们会捕获我们返回的内容。

我们在 window.returnValue 中加载我们的响应,因此他们将它放在“result”变量中,但是如果他们用 X 按钮关闭我们的窗口,他们永远不会得到我们的响应,尽管我们已经设置了它。如果他们通过点击我们的“接受”按钮关闭我们的窗口,他们就会得到响应。

如果我尝试捕获卸载事件,然后我再次设置 window.returnValue,他们也不会得到它。

为什么会发生这种情况,我如何确保即使他们按下 X 也能获得返回值?

我无法改变他们如何称呼我们的网络应用程序,我知道这很糟糕,因为这是一种愚蠢的做事方式......

他们只使用 Internet Explorer,并以 IE 5 模式运行...

【问题讨论】:

  • 可以分享showModalDialog函数的内容吗?
  • showModalDialog 不是我开发的功能:developer.mozilla.org/es/docs/Web/API/Window/showModalDialog
  • 因为close modal是异步的,当modal关闭时需要使用promise和回调函数来执行事件。使用确切的代码,您只能使用停止执行 JavaScript 的默认警报/确认/提示函数,其他所有函数都不会阻止执行。

标签: javascript html internet-explorer


【解决方案1】:

如果可能的话,您可以尝试使用onbeforeunload event 进行测试

这样,您可以尝试向用户显示提示,并告知他们如果继续使用此方法,他们可能会丢失在此页面上所做的更改。然后你可以用正确的步骤引导用户。

例子:

window.onbeforeunload = confirmExit;
function confirmExit()
{
    return "You have attempted to leave this page.  If you have made any changes to the fields without clicking the Accept button, your changes will be lost.  Are you sure you want to exit this page?";
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-09
    • 2012-10-29
    • 1970-01-01
    • 2022-01-22
    相关资源
    最近更新 更多