javascript - showModalDialog 在 Chrome 中不返回值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10213530/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javascript - showModalDialog not returning value in Chrome
提问by ConnorsFan
I made a small calendar popup in Javascript. Very simple, using the Calendar control from ASP.NET. I call the popup window with showModalDialog. In the modal window, changing the current month of the calendar causes problems because of the postback, and I found in several places that the solution is to put:
我用 Javascript 做了一个小日历弹出窗口。非常简单,使用来自 ASP.NET 的 Calendar 控件。我用 showModalDialog 调用弹出窗口。在模态窗口中,更改日历的当前月份因为回发导致出现问题,我在几个地方找到了解决方案:
<base target="_self"/>
in the head part of the aspx file. Everything works great... except for one thing, and only in Google Chrome. To get back the selected date, I set the returnValue of the popup to the date selected in the calendar. In IE and Firefox, it always works. In Chrome, however, it works only if I don't change the current month in the calendar. As soon as I change it, the return value is not passed back to the caller of showModalDialog. It is as if the modal window is not the original one anymore; the return value is undefined.
在 aspx 文件的头部。一切都很好......除了一件事,而且只在谷歌浏览器中。为了取回选定的日期,我将弹出窗口的 returnValue 设置为日历中选定的日期。在 IE 和 Firefox 中,它始终有效。但是,在 Chrome 中,它仅在我不更改日历中的当前月份时才有效。一旦我改变它,返回值就不会传回给 showModalDialog 的调用者。就好像模态窗口不再是原来的窗口一样;返回值未定义。
Has anyone experienced that behavior and have a suggestion to make it work? I tried using dialogArguments to keep trace of the caller window but it gets passed only to the first modal window (it is lost after changing the current month).
有没有人经历过这种行为并有建议让它发挥作用?我尝试使用 dialogArguments 来跟踪调用者窗口,但它仅传递到第一个模式窗口(更改当前月份后丢失)。
The code in the calling procedure:
调用过程中的代码:
var d = window.showModalDialog(...)
The code in the modal window:
模态窗口中的代码:
window.returnValue = selectedDate;
self.close();
As I said to Teemu, selectedDate and window.returnValue are both always correct. However, in the case of Google Chrome (after a month change in the calendar), returnValue is not passed back by showModalDialog and d is undefined.
正如我对 Teemu 所说的, selectedDate 和 window.returnValue 都是正确的。但是,在 Google Chrome 的情况下(在日历更改一个月后),showModalDialog 不会传回 returnValue,并且 d 未定义。
回答by ConnorsFan
In order to keep using showModalDialog in my page, I had to come up with my own workaround for the bug. So, here it is...
为了在我的页面中继续使用 showModalDialog,我必须想出我自己的解决方法来解决这个错误。所以,这里是...
In Google Chrome, after a postback, showModalDialog always returns undefined. However, the window.opener property in the modal dialog points to the caller window, even after postbacks. So, I thought about putting the result of the dialog in the returnValue property of that caller window. And it works.
在 Google Chrome 中,在回发后,showModalDialog 总是返回 undefined。但是,模式对话框中的 window.opener 属性指向调用者窗口,即使在回发之后也是如此。所以,我想把对话框的结果放在那个调用者窗口的 returnValue 属性中。它有效。
In the caller window:
在调用者窗口中:
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
In the modal dialog window:
在模态对话框窗口中:
if (window.opener)
{
window.opener.returnValue = dateValue;
}
window.returnValue = dateValue;
self.close();
回答by Bachask8
I had this same error, what i found in some forum is that if you put your controls in a updatePanel and ContentTemplate it will work:
我遇到了同样的错误,我在某个论坛中发现,如果您将控件放在 updatePanel 和 ContentTemplate 中,它将起作用:
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
</ContentTemplate>
</asp:UpdatePanel>