Solved

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

Posted on 2013-11-06
3
2,624 Views
Last Modified: 2013-11-11
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
Comment
Question by:kev1970
3 Comments
 
LVL 52

Assisted Solution

by:Scott Fell, EE MVE
Scott Fell,  EE MVE earned 100 total points
ID: 39627182
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
 
LVL 19

Accepted Solution

by:
Albert Van Halen earned 400 total points
ID: 39632949
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
 

Author Comment

by:kev1970
ID: 39638444
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now