【转载】showModalDialog returnValue is undefined in Google Chrome

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:

1
2
3
4
5
6
7
<script language="javascript" type="text/javascript">
  if (window.opener) {
    window.opener.returnValue = "your return value";
  }
  window.returnValue = "your return value";
  self.close();
</script>

Then the window calling the modal window:

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

1
2
3
4
5
6
7
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:





posted @ 2014-04-02 11:37  飞起航  阅读(2512)  评论(0编辑  收藏  举报