问题描述
我在Javascript中制作了一个小日历弹出窗口。非常简单,使用ASP.NET中的Calendar控件。我用showModalDialog调用弹出窗口。在模态窗口中,更改日历的当前月份会因为回发而导致问题,我在几个地方找到了解决方案:
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:
<base target="_self"/>
在aspx文件的头部。一切都很好......除了一件事,只有谷歌Chrome。要返回所选日期,我将弹出窗口的returnValue设置为日历中选定的日期。在IE和Firefox中,它始终有效。但是,在Chrome中,仅当我不更改日历中的当前月份时,它才有效。一旦我更改它,返回值就不会传递回showModalDialog的调用者。好像模态窗口不再是原始窗口了;返回值未定义。
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.
有没有人遇到过这种行为并有建议让它起作用?我尝试使用dialogArguments来跟踪调用者窗口但它只传递给第一个模态窗口(它在更改当前月份后丢失)。
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).
调用程序中的代码:
var d = window.showModalDialog(...)
模态窗口中的代码:
window.returnValue = selectedDate;
self.close();
正如我对Teemu说的那样,selectedDate和window.returnValue都是正确的。但是,对于谷歌浏览器(日历中的一个月更改)后,showModalDialog不会返回returnValue,并且未定义d。
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.
推荐答案
为了在我的页面中继续使用showModalDialog,我不得不为bug提出自己的解决方法。所以,这里是...
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始终返回undefined。但是,即使在回发之后,模态对话框中的window.opener属性也指向调用者窗口。所以,我考虑将对话框的结果放在该调用者窗口的returnValue属性中。它有效。
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.
在来电者窗口中:
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中返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!