Solved

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

Posted on 2013-11-06
3
2,734 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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

828 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