Con*_*Fan 18 javascript return-value showmodaldialog
我在Javascript中制作了一个小日历弹出窗口.非常简单,使用ASP.NET中的Calendar控件.我用showModalDialog调用弹出窗口.在模态窗口中,由于回发,更改日历的当前月份会导致问题,我在几个地方找到了解决方案:
<base target="_self"/>
Run Code Online (Sandbox Code Playgroud)
在aspx文件的头部分.一切都很好......除了一件事,只有谷歌Chrome.要返回所选日期,我将弹出窗口的returnValue设置为日历中选定的日期.在IE和Firefox中,它始终有效.但是,在Chrome中,仅当我不更改日历中的当前月份时,它才有效.一旦我更改它,返回值就不会传递回showModalDialog的调用者.好像模态窗口不再是原始窗口了; 返回值未定义.
有没有人经历过这种行为,并有建议让它发挥作用?我尝试使用dialogArguments来跟踪调用者窗口但它只传递给第一个模态窗口(它在更改当前月份后丢失).
调用过程中的代码:
var d = window.showModalDialog(...)
Run Code Online (Sandbox Code Playgroud)
模态窗口中的代码:
window.returnValue = selectedDate;
self.close();
Run Code Online (Sandbox Code Playgroud)
正如我对Teemu所说,selectedDate和window.returnValue都是正确的.但是,对于谷歌浏览器(在日历中更改一个月后),showModalDialog不会返回returnValue,并且未定义d.
Con*_*Fan 24
为了在我的页面中继续使用showModalDialog,我不得不为bug提出自己的解决方法.所以,这里是......
在谷歌浏览器中,回发后,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
Run Code Online (Sandbox Code Playgroud)
在模态对话框窗口中:
if (window.opener)
{
window.opener.returnValue = dateValue;
}
window.returnValue = dateValue;
self.close();
Run Code Online (Sandbox Code Playgroud)