2012-04-18 91 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>