2

As in the question... how to get the window object from an event fired in the window scope for example:

handleEvent: function(event) {

  // is window object available here and can we get it from event
}

I can get the window object from other APIs. I was wondering if it was possible to get it from the fired event.

Reference:
handleEvent
Code Snippet using handleEvent

erosman
  • 7,094
  • 7
  • 27
  • 46

3 Answers3

4

I found out the answer ... any of these will get the window object from the event

event.view event.view
event.target.ownerDocument.defaultView event.target
event.originalTarget.ownerGlobal event.originalTarget (Non-standard)

erosman
  • 7,094
  • 7
  • 27
  • 46
  • That's what it is! event.view awesome! thanks man! Really like that non-standard tip as well. – Noitidart Sep 22 '14 at 21:18
  • This is not guaranteed to work, but it will work almost all the time. It assumes that the event was fired on a GUI/DOM target (i.e. from within a browser). There are possibilities for which this may not be the `window` object. – Makyen Sep 22 '14 at 22:07
  • @Makyen ... maybe... but I mean form a window environment... I will clarify my question – erosman Sep 23 '14 at 04:52
  • I had a "scroll" event listener and the handler function receives an `event` object. `event.target.defaultView` returned the `window` object. – Mr. N Jun 25 '22 at 16:53
2

It depends on the event. But most usually yes you can. Do a console.log on the event then you might something like targetChromeWindow or something, this one I can't remember i came across it once though while doing something.

Most usually though, get event.target or relatedTarget or originalTarget (theres one more target i forgot what it is) and do ownerDocument.defaultView

If you want the chrome window from that you can get that by doing this:

var DOMWin = win.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIWebNavigation)
.QueryInterface(Ci.nsIDocShellTreeItem)
.rootTreeItem
.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIDOMWindow);
Noitidart
  • 35,443
  • 37
  • 154
  • 323
  • Thank you... that code is too long .. I can get the window with `Services.ww.activeWindow` easily .. I wanted to know if it was possible from the fired event ;) – erosman Sep 22 '14 at 18:31
  • Check out the event.target and others i listed abouve then doing a .ownerDocument.defaultView on it – Noitidart Sep 22 '14 at 19:06
1

The following will populate the window and document variables if they do not already exist. It should work from any scope/context:

if (typeof window === "undefined") {
    //If there is no window defined, get the most recent.
    var window = Components.classes["@mozilla.org/appshell/window-mediator;1"]
                           .getService(Components.interfaces.nsIWindowMediator)
                           .getMostRecentWindow("navigator:browser");
}

if (typeof document === "undefined") {
    //If there is no document defined, get it
    var document = window.content.document;
}

Here are some additional variables which might be useful to have available, depending on what you are doing:

if (typeof gBrowser === "undefined") {
    //If there is no gBrowser defined, get it
    var gBrowser = window.gBrowser;
}

var tab = gBrowser.selectedTab;
var browserForTab = gBrowser.getBrowserForTab( tab );
var notificationBox = gBrowser.getNotificationBox( browserForTab );
var ownerDocument = gBrowser.ownerDocument;
Makyen
  • 31,849
  • 12
  • 86
  • 121