3

Is it possible to close a modal window from it's parent? It is a little hard even to try to do this. Basically, I am opening a non-modal window. From that non-modal window the user might some times open a modal window. Now suppose I close the non-modal window... I would like the modal subwindow to close also. How can this be done?

Basically, overall, I have to find a way to close a modal window from it's parent.

Thanks, Grae

Here is some code to give you a sample.

The Start Page


    <html>
   <head>
     <script>
        window.childWindow;
        function openNonModal()
        {
           window.childWindow = window.open("nonmodal.html","_blank", "dialogWidth=500px;dialogHeight=294px;scroll=no;status=no;caption=no;titlebar=no;menubar=no;toolbar=no;help=no");
           setTimeout("closeChildWindow()",5000);
        }

    function closeChildWindow()
    {
       window.childWindow.closeChildWindow();
       window.childWindow.close();
    }
 <script>

</head> <input type="button" value="openNonModal" onclick="openNonModal(); return false;"/> </html>

The nonModal window which open a modal window.


    <html>
   <head>
     <script>
        function openModal()
        {
           var retVal = window.showModalDialog("modal.html","_blank", "dialogWidth=500px;dialogHeight=294px;scroll=no;status=no;caption=no;titlebar=no;menubar=no;toolbar=no;help=no");
        }
        function closeChildWindow()
        {
           alert("should close child window");

    }
 </script>

</head> <input type="button" value="openModal" onclick="openModal(); return false;"/> </html>

The window that is modal is just text.


    <html>
   Just a sample.
</html>
GC_
  • 1,673
  • 6
  • 23
  • 39
  • `Now suppose I close the non-modal window... I would like the modal subwindow to close also. How can this be done?` shouldn't that be the default behaviour? What happens at the moment - does the modal window in fact stay orphaned? – Pekka Sep 03 '10 at 15:40
  • 2
    The very concept of a modal window implies that the parent window should not be manipulated until the modal window is addressed. http://en.wikipedia.org/wiki/Modal_window EDIT: From Wikipedia: "In user interface design, a modal window is a child window that requires users to interact with it before they can return to operating the parent application, thus preventing the workflow on the application main window." – Matthew Sep 03 '10 at 15:41
  • @Pekka The current behavior is that IE doesn't close the non- modal window, if it has a modal sub-window open. My plan is to close the modal window first, so that I can close it's parent the non-modal window. – GC_ Sep 03 '10 at 16:03
  • @Matthew PK In theory, maybe, that is not really the case. For example, you have two IE windows open, and then you click on a link that opens a modal window. The modal window will block that instance of IE, but the other instance is still free go to other links. – GC_ Sep 03 '10 at 16:05
  • @Matthew well, that is good and true, but what we're talking about here is how the *browsers* interpret modal windows, and what they allow to happen to the parent window. (@Grae I'm stymied myself that the parent window can be closed before the modal child, though. What browser(s) is this in?) – Pekka Sep 03 '10 at 16:06
  • @Grae aah, you mean the modal window's *grandparent*? – Pekka Sep 03 '10 at 16:07
  • OK, grandparent tries to close parent, however parent has a modal child. Therefor, parent cannot close. My idea is to close the modal window (grandchild) first, so that I can close parent. – GC_ Sep 03 '10 at 16:09
  • I can run code on parent, from grandparent, however, I am not sure what code to run. – GC_ Sep 03 '10 at 16:10
  • I have no how to reference grandchild, in order to get it to close. – GC_ Sep 03 '10 at 16:10
  • @Pekka I missed you one comment. You right, the parent cannot be closed before the modal child. However, the doesn't stop me from wanting it to close. I am basically trying to close the modal child from the grandparent/parent before I close the parent. – GC_ Sep 03 '10 at 16:20
  • @Grae I see! I can think of no simple solution, seeing as code execution in the parent Window (I think) is "frozen" until the modal closes. Tricky! – Pekka Sep 03 '10 at 16:21
  • @Pekka I can get code to run in the parent from the grandparent. I can really run any function I like, at least in IE. However, I don't know what I should run in the code. – GC_ Sep 03 '10 at 16:30
  • @Grae I see! There is no reference to the modal child in the parent window, of course. Hmmmmm... Then the only thing that comes to mind is to do some kind of Ajax polling from inside the modal window to get a "close" signal. Yuck! Possible, but awfully complex. – Pekka Sep 03 '10 at 16:34

1 Answers1

2

As soon as you open a modal window, the opener freezes and you can't apply any control via it!

for more clarification take a look on this code:

setTimeout('alert(1);', 1000);
setTimeout('alert(2);', 2000);
setTimeout('window.showModalDialog(...);', 3000);
setTimeout('alert(3);', 4000);

alert(1) will be executed on the first second.

alert(2) will be executed on the next second even if you don't confirm the alert(1).

on the 3rd second the modal window will be opened and at this point the opener window freezes, so alert(3) will not be executed on the 4th second.

alert(3) wont be executed when the modal window is still open, but one second after closing the modal window, alert(3) will be executed.

Conclusion: Parent has no control on the modal window after opening, because it's actually dead :-(

Ehsan
  • 1,937
  • 1
  • 17
  • 30