Solved

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

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

763 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

7 Experts available now in Live!

Get 1:1 Help Now