Search code examples
javascriptfirefox-addonobject-literal

Object Literal in Firefox extension


I have an extension that needs to pop up a window and then close it.

 var my_extension = {
    window: null,
    popup: function(){
       my_extension.window = window.open(...)
    },
    close: function(){
       my_extension.window.close()
    }
 }

The popup calls the close function. However, after returning from the open, the my_extension.window is null. I check to make sure it is set in popup. Is another instance of my_extension created when the popup returns?


Solution

  • my_extension is defined in the main browser window, not in the popup. To close the popup from the popup itself, just use window.close

    edit: ok, so I guess my_extension.close actually looks something like:

    function() {
      // check input from popup window
      if (everythingIsGood) {
        my_extension.window.close()
      }
    }
    

    In that case, I would recommend you do that validation in the popup itself. I know, you don't want to put a lot of code in the popup window. And I agree. But you can pass in whatever information is necessary to do the validation (including passing a validation function -- remember that functions are objects too, because JavaScript is cool like that!) when you open the popup. Look for the discussion of window.arguments on this page: https://developer.mozilla.org/en/DOM/window.openDialog