views:

528

answers:

3

I tried this but failed:

var win = showModalDialog('http://localhost/index.php');
win.close();
+1  A: 

When you execute showModalDialog, the entire code sequence is blocked. You need to close the modal window to proceed, however win will be null by then :P

o.k.w
+4  A: 

The definition of a modal window is that execution of the current function stops until the modal window is closed. That is, the call to showModalDialog() will block until the shown dialog is closed. Therefore, your win.close() will be called after the window is already closed (not what you're intending).

You have a couple options:

  • Show the dialog as non-modal and wait in an events loop until a certain condition is met. Then, close the window from the calling function.

  • The modal dialog closes itself at an appropriate time.

lc
Then is there any way to close the modal dialog with script?
Mask
No you can't close it, because it's modal...
sdwilsh
Well, the dialog could *close itself* with `window.close()`, but just as @sdwilsh says, you can't do anything from outside of it.
lc
A: 

Modal dialog means that next operator is not executed UNTIL the dialog is closed. This is why nothing you place in the next line will ever work.

That's the purpose of modal dialogs - to freeze current window and get some mandatory input from the user. If you want to close it immediately, I suspect that you don't really need a modal dialog.

By the way, return value of showModalDialog is dialog return code, and not a window variable!

Normally, modal dialogs are closed from within. If you don't want to wait for user's input, there must be something in index.php code that closes it.

yu_sha
How to close it from within,can you be more specific?
Mask
You could have onLoad event in the index.php that would set up a timer and call window.close after a while.What are you trying to achieve? Window that blinks and closes? Why?
yu_sha