• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3164
  • Last Modified:

How do I close a form opened with window.showModalDialog?

I've opened a form with:
      newWindow = window.showModalDialog("\\Test1.asp")
      newWindow.focus();

In Test1.asp, I have a "close" hyperlink:
      <a href=# onclick='javascript:self.close();'>close</a>

When I click on the hyperlink it opens the same form in another another browser and displays the error message:
"The webpage you are viewing is trying to close the window
Do you want to close this window?""

If I open the form with:
      newWindow = window.open("\\Test1.asp;
      newWindow.focus();

then Test1.asp closes ok.

I need to only open 1 instance of this form so how do I close a form opened with window.showModalDialog?
If it's not possible then is there a better method?
0
kev1970
Asked:
kev1970
2 Solutions
 
Scott Fell, EE MVEDeveloperCommented:
See if this sample helps https://developer.mozilla.org/samples/domref/showModalDialog.html

<html>
<head>
<script>
function openModal()
{
  var a = new Array;
  a[0] = 1;
  a[1] = 4;

  var r = window.showModalDialog('http://developer.mozilla.org/samples/domref/showModalDialogBox.html',
      a, "dialogwidth: 450; dialogheight: 300; resizable: yes");
  document.getElementById('foo').textContent = r;
  alert(r);
}
</script>
</head>
 
<body>
<input type="button" value="Open modal dialog" onclick="openModal();">
<div>
<p>Modal dialog return value:</p>
<p id="foo">
</div>
</body>
</html>

Open in new window

modal
<html>
<body>
<script>
document.write("Modal dialog got argument: " + window.dialogArguments);
</script>
<input id="foo" type="text" value="Dialog value...">
<input type="button" value="Close" onclick="window.returnValue = document.getElementById('foo').value; window.close();">
<a href="safe.html">link</a>
</body>
</html>

Open in new window

0
 
Albert Van HalenCommented:
Be aware that showModalDialog only works (correctly) in IE.
Script execution within the opening window stops, so you cannot close the window from there.
You can only close the window using the close icon in the right upper corner of the dialog, or by javascript (which you already have).

The fix for your link within the dialog is simple.
Put this in the head tag of your modal
<base target="_self" />

Open in new window

0
 
kev1970Author Commented:
I used <base target="_self" /> in the dialog form and window.returnValue to return the info to the caller before window.close();
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now