3
votes

I need to open a modal dialog after the Firefox extension is launched, and then close it after certain conditions.

I defined the dialog with an id = 'myWindowName' in the XUL file, and then launch it using window.openDialog('xul url', 'myWindowName', ...) directly without assigning it to any variable.

Then, I define y = document.getElementById('myWindowName') in a later function and use y.cancelDialog() to close it.

So far, it looks as though it's working, but I wanted to ask if this method is correct.

Does y=window.openDialog("url', 'myWindowName', ...) return the same reference as y=document.getElementByID('myWindowName') ?

Also, why doesn't window.cancelDialog() work? Is window.close() a better option to autoclose the dialog?

1

1 Answers

4
votes

Read Working with windows in chrome code.

It seems that you don't know that there is more than a single "scope" in Firefox (and in regular web pages). What I mean is: every window, tab, and iframe has its own set of objects (global object, which is the Window object, document etc). In addition to that sometimes there are "scopes" with their own global object, but no DOM (no window or document) - XPCOM components, jsm modules, workers, sandboxes.

openDialog returns the window object of the new dialog. The <dialog id='myWindowName'> element can be accessed in that window's document, the syntax depends on where the script, that wants to access it, runs: if it runs in the dialog, it can use document.getElementById(), while a script in another window has to access it through a reference to the dialog's window (say, dialogWindow): dialogWindow.document.getElementById().

cancelDialog is a method on the XUL <dialog> element, while close() is a window's method, which is why window.cancelDialog() doesn't work, while window.close() does.

As to which of dialog.cancelDialog or window.close() is "better": cancelDialog does more than a simple window.close() (see its code on mxr) -- the same steps as when the Cancel button on the dialog is pressed. So before closing the window, it checks if the dialog's Cancel button is disabled, then fires ondialogcancel event letting interested code to prevent that from happening, then if everything's ok, it closes the window via window.close().