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

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

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 ="\\Test1.asp;

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?
Who is Participating?
Albert Van HalenConnect With a Mentor Analyst developerCommented:
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

Scott Fell, EE MVEConnect With a Mentor Developer & EE ModeratorCommented:
See if this sample helps

function openModal()
  var a = new Array;
  a[0] = 1;
  a[1] = 4;

  var r = window.showModalDialog('',
      a, "dialogwidth: 450; dialogheight: 300; resizable: yes");
  document.getElementById('foo').textContent = r;
<input type="button" value="Open modal dialog" onclick="openModal();">
<p>Modal dialog return value:</p>
<p id="foo">

Open in new window

document.write("Modal dialog got argument: " + window.dialogArguments);
<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>

Open in new window

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

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.