tags:

views:

116

answers:

3

Hi, I am using the modal dialog box to open the modal dialog. Here is the code

var Window;

function PopDis()
{
    Window=window.showModalDialog('/collector/modalBox.jsp', '', 'dialogHeight:300px; dialogWidth:500px;scroll:no; status:no; help:no; center:yes; resizable:no'); 

}

In another function i want to close the modal dialog. Code is

function CloseModalDialog()
{
    alert("in fun close"+Window);
    Window.close();
}

But var Window is undefined hence unable to close the window. Please give me the solution.

A: 

That means the variable Window is out of scope.

Without knowing the rest of the code one quick way to fix it would be using the window variable. Like this:

function PopDis()
{
    window.win = window.showModalDialog('/collector/modalBox.jsp', 'win', 'dialogHeight:300px; dialogWidth:500px;scroll:no; status:no; help:no; center:yes; resizable:no');
}

In another place that has access to window (the browser window, not your modal window):

function CloseModalDialog()
{
    alert("in fun close"+win);
    window.win.close();
    window.win = undefined;
}

It's not pretty and most people don't recommend using this, though.

Edit: The window variable of the browser's javascript is usually global. So this should work.

Maushu
+1  A: 

The showModalDialog is a blocking call. No other operation on the parent page will be possible till the dialog itself is closed.

So, even if you call a javascript in the next line after ShowModalDialog() it will not be executed till the dialog is actually closed...

You will be able to close the dialog from the page which is shown in the dialog (assuming you have control to change the code in the page shown). But nothing will be excuted on the parent page till the modal dialog is closed.

HTH

Sunny
A: 

To elucidate on Sunny's answer (+1), the Window variable will not even be assigned until showModalDialog has finished, which doesn't happen until the dialog window has closed.

In any case, showModalDialog does not return a window object — obviously, for the reason above, it would be pointless to do so. It returns the “returnValue” given by the code in the dialog itself. The dialog can only be closed by code inside the dialog, which doesn't need a Window variable because it can just use its own window global.

If you need to interact with a dialog from outside its document, what you want isn't a modalDialog.

If you want a user experience that doesn't totally suck, what you want isn't a modalDialog.

bobince
Is this possible with javascript session? If yes how to do it?
Aru
What do you mean by a ‘JavaScript session’? Sessions are normally a server-side feature.
bobince