2014-09-22 79 views
1

如問題...如何從window範圍例如觸發的事件得到window對象:是否有可能從handleEvent中的事件獲取窗口對象?

handleEvent: function(event) { 

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

我可以從其他API的window對象。我想知道是否有可能從被解僱的event中得到它。

參考:
handleEvent
Code Snippet using handleEvent

+0

你真的應該提供一些更多的上下文在這個問題上。 – erikvold 2014-09-22 22:34:00

+0

確定可以刪除評論。 – Noitidart 2014-09-25 12:59:12

回答

3

我找到了答案......所有這些都會從事件

event.viewevent.view
獲取window對象event.target.ownerDocument.defaultViewevent.target
event.originalTarget.ownerGlobalevent.originalTarget(非標)

+0

這就是它! event.view真棒!謝啦!真的很喜歡那個非標準的提示。 – Noitidart 2014-09-22 21:18:50

+0

這不能保證能夠正常工作,但幾乎可以一直工作。它假定該事件是在GUI/DOM目標(即從瀏覽器內)觸發的。這可能不是'window'對象。 – Makyen 2014-09-22 22:07:03

+0

@Makyen ......也許......但我的意思是形成一個窗口環境...我會澄清我的問題 – erosman 2014-09-23 04:52:01

1

這取決於事件。但通常情況下,你可以。在事件上做一個console.log,然後你可能會像targetChromeWindow之類的東西,這個我不記得我在做某事的時候遇到過它。

最通常不過,讓event.target或relatedTarget或originalTarget(那裏有一個以上目標我忘了是什麼),如果你想在Chrome窗口,從你可以得到做做ownerDocument.defaultView

這樣的:

var DOMWin = win.QueryInterface(Ci.nsIInterfaceRequestor) 
.getInterface(Ci.nsIWebNavigation) 
.QueryInterface(Ci.nsIDocShellTreeItem) 
.rootTreeItem 
.QueryInterface(Ci.nsIInterfaceRequestor) 
.getInterface(Ci.nsIDOMWindow); 
+0

謝謝...代碼太長..我可以很容易地通過'Services.ww.activeWindow'獲得窗口..我想知道是否可以從被觸發的事件中獲得;) – erosman 2014-09-22 18:31:08

+0

查看event.target和其他人我列出abo​​uve,然後做一個.ownerDocument.defaultView – Noitidart 2014-09-22 19:06:33

1

,如果他們不存在,下面將填充窗口和文檔的變量。

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; 
} 

這裏有一些額外的變量,這可能是有用的可用,這取決於你在做什麼:它應該可以在任何範圍/環境中工作

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; 
相關問題