3
votes

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>
1
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
The very concept of a modal window implies that the parent window should not be manipulated until the modal window is addressed. 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
@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_
@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_
@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

1 Answers

2
votes

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 :-(