gpt4 book ai didi

javascript - showModalDialog 未在 Chrome 中返回值

转载 作者:行者123 更新时间:2023-12-03 13:29:39 27 4
gpt4 key购买 nike

我用 Javascript 制作了一个小的日历弹出窗口。很简单,使用 ASP.NET 中的 Calendar 控件。我用 showModalDialog 调用弹出窗口。在模态窗口中,更改日历的当前月份会因为回发而出现问题,我在几个地方发现了解决方案是:

<base target="_self"/>

在 aspx 文件的头部。一切都很好......除了一件事,而且只在谷歌浏览器中。为了取回选定的日期,我将弹出窗口的 returnValue 设置为日历中选定的日期。在 IE 和 Firefox 中,它始终有效。但是,在 Chrome 中,它仅在我不更改日历中的当前月份时才有效。一旦我更改它,返回值就不会传回给 showModalDialog 的调用者。就好像模态窗口不再是原来的窗口了;返回值未定义。

有没有人经历过这种行为并有建议让它发挥作用?我尝试使用 dialogArguments 来跟踪调用者窗口,但它只传递到第一个模式窗口(更改当前月份后它会丢失)。

调用过程中的代码:
var d = window.showModalDialog(...)

模态窗口中的代码:
window.returnValue = selectedDate; 
self.close();

正如我对 Teemu 所说, selectedDate 和 window.returnValue 总是正确的。但是,在 Google Chrome 的情况下(在日历中更改一个月后),showModalDialog 不会将 returnValue 传回,并且 d 是未定义的。

最佳答案

为了继续在我的页面中使用 showModalDialog,我必须想出我自己的解决方法来解决这个错误。所以,这里...

在 Google Chrome 中,回发后,showModalDialog 总是返回 undefined。但是,模式对话框中的 window.opener 属性指向调用者窗口,即使在回发之后也是如此。所以,我考虑将对话框的结果放在调用者窗口的 returnValue 属性中。它有效。

在来电窗口中:

var prevReturnValue = window.returnValue; // Save the current returnValue
window.returnValue = undefined;
var dlgReturnValue = window.showModalDialog(...);
if (dlgReturnValue == undefined) // We don't know here if undefined is the real result...
{
// So we take no chance, in case this is the Google Chrome bug
dlgReturnValue = window.returnValue;
}
window.returnValue = prevReturnValue; // Restore the original returnValue

At this point, use dlgReturnValue for further processing

在模态对话框窗口中:
if (window.opener)
{
window.opener.returnValue = dateValue;
}
window.returnValue = dateValue;
self.close();

关于javascript - showModalDialog 未在 Chrome 中返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10213530/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com