showModalDialog returnValue is undefined in Google Chrome

For some reason, when using a Javascript showModalDialog from an ASP.NET project, Google Chrome will return an empty (“undefined”) returnValue. This error has been known by the Google Chromium team since 2010, and they have yet to fix it.

Fortunately there is a workaround.

First the modal window:

When closing the modal window it is not enough to set the window.returnValue to the specified return value. Instead, check for the window.opener and set the returnValue there as well:

<script language="javascript" type="text/javascript">
  if (window.opener) {
    window.opener.returnValue = "your return value";
  window.returnValue = "your return value";

Then the window calling the modal window:

To receive the return value from the modal window you need to do this:

window.returnValue = undefined;
var result = window.showModalDialog("modalwindow.aspx", window, "dialogHeight:650px; dialogWidth:900px;");
  if (result == undefined)
    result = window.returnValue;
  if (result != null && result != "undefined")
    // Do something with the return value
    // defined in "result"

This has beed tested in IE9 and Google Chrome 21.0.1180.83 m. According to other sources it will work in later Firefox versions as well.

Further reading:

About briancaos

Developer at Pentia A/S since 2003. Have developed Web Applications using Sitecore Since Sitecore 4.1.
This entry was posted in Uncategorized and tagged , , , . Bookmark the permalink.

4 Responses to showModalDialog returnValue is undefined in Google Chrome

  1. Is there the same bug in Safari and other browsers based on WebKit?

  2. gegal says:

    You should replace
    ‘if (result != null && result != “undefined”)’
    ‘if (result != null && result != undefined)’

  3. fishypeople says:

    It just doesn’t seem to work in Version 23.0.1271.97 of Chrome.

    If I comment out the self.close as follows, and put alerts in before and after the return value is set then both alerts show as ‘undefined’:

    if (window.opener)
    window.opener.returnValue = “your return value”;

  4. Pingback: showModalDialog returnValue is undefined in Google Chrome.. in Sitecore | Paul's Sitecore Musings

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s