2012-04-18 26 views
19

我在Javascript中创建了一个小日历弹出窗口。非常简单,使用ASP.NET的Calendar控件。我用showModalDialog调用弹出窗口。在模态窗口,更改日历的当前月份会导致因为回传的问题,我在几个地方发现,解决的办法是把:javascript - showModalDialog在Chrome中不返回值

<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的情况下(在日历更改一个月后),returnValue不会被showModalDialog传回,并且d是未定义的。

+0

听起来更像你的程序来改变模式对话框中的'returnValue'在Chrome中失败。 – Teemu 2012-04-18 16:24:59

+0

在这种情况下,为什么当我留在当前月份时它会​​工作? – ConnorsFan 2012-04-18 16:26:06

+0

很难说没有看到代码... – Teemu 2012-04-18 16:27:22

回答

24

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

在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(); 
+5

+1努力工作。你真的走过了错误。我昨天晚上试图重现你的问题,但是即使没有改变对话框,我也无法得到'returnValue'... – Teemu 2012-04-19 20:52:13

+1

我认为[这是原始的Chromium缺陷](http://code.google.com/p/)铬/问题/细节?id = 42939) – robertc 2012-05-22 15:50:27

+0

仅供参考,差不多两年后,Chrome仍然需要这种解决方法。 – SouthShoreAK 2014-01-15 23:38:56

0

我有同样的错误,我在一些论坛上发现的是,如果你把你的控件在它将工作:

<asp:UpdatePanel ID="UpdatePanel1" runat="server"> 
      <ContentTemplate> 
      </ContentTemplate> 
</asp:UpdatePanel>