Javascript 从选项卡加载事件MDN获取原始目标

Javascript 从选项卡加载事件MDN获取原始目标,javascript,firefox,web,firefox-addon,Javascript,Firefox,Web,Firefox Addon,我正按照以下示例尝试获取originalTarget DOM: gBrowser.addEventListener("load", function(aEvent){ s.handleLoadBrowser(aEvent);}, true); handleLoadBrowser : function (aEvent){ var w = aEvent.originalTarget.defaultView; } 我为选项卡加载创建一个事件侦听器: var tab = gBrows

我正按照以下示例尝试获取originalTarget DOM:

gBrowser.addEventListener("load", function(aEvent){ s.handleLoadBrowser(aEvent);},     true);

handleLoadBrowser : function (aEvent){
    var w = aEvent.originalTarget.defaultView;
}
我为选项卡加载创建一个事件侦听器:

var tab = gBrowser.addTab("www.google.com");
tab.addEventListener("load", function(aEvent){ s.handleLoadTab(aEvent) }, true);

handleLoadTab : function (aEvent){
    var w = aEvent.originalTarget.defaultView;
} 
这里我得到一个错误:TypeError:win未定义


如何从选项卡事件加载中获取此dom对象?

关于您的第一个示例,它应该可以工作:我做了类似的操作:我唯一的区别是:gBrowser.addEventListenerload,s.handleLoadBrowser,true

关于第二个示例:var tab=gBrowser.addTabwww.google.com; tab.addEventListenerload,functionEvent{s.handleLoadTabaEvent},true;您不能这样做,您不需要向tab添加加载事件,tab是一个xul元素,您可以抓取并移动它。您可以执行tab.linkedBrowser.contentWindow.addEventListener'load'操作,但在加载第一页后,它将不再存在

此代码适用于我:

var tab = gBrowser.addTab("data:text/html,<b>hi</b>");
tab.linkedBrowser.contentWindow.addEventListener("load", handleLoadTab, true);
//tab.linkedBrowser.contentWindow.addEventListener("DOMContentLoaded", handleLoadTab, true);

function handleLoadTab(aEvent){
    var w = aEvent.originalTarget.defaultView;
  w.alert('load done')
} 

包含文件和行号的实际完整错误消息是什么?显示的错误表明变量win未定义。您提供的代码不包含win变量。因此,您没有包含实际导致您看到的错误的代码。请包括实际导致错误的代码。注意:提供的代码不反映代码的结构,因为s.handleLoadTab不是handleLoadTab。我们必须假设s的目标。请参阅:
var tab = gBrowser.loadOneTab('data:text/html,<span class="profilist-build-icon-1">backround of this span is of icon on desktop</span><input type="button" value="applyCss"><input type="button" value="removeCss"> Change File on Desktop to: <input type="button" value="Release Img"><input type="button" value="Beta Img"><input type="button" value="Aurora Img"><input type="button" value="Nightly Img">', {inBackground:false});
    var mobs = new window.MutationObserver(function(mutations) {
        mutations.forEach(function(mutation) {
            var attrVal = tab.getAttribute(mutation.attributeName);
            console.log(mutation.attributeName, attrVal);
            if (mutation.attributeName == 'progress' && attrVal == 'true') {
              //at this point can addEventListener for DOMConentLoaded and it will as document hasnt loaded yet
              tab.linkedBrowser.contentDocument.addEventListener('DOMContentLoaded', function() {
                tab.linkedBrowser.contentDocument.removeEventListener('DOMContentLoaded', arguments.callee, false);
                alert('loadOneTab finished loading', tab.linkedBrowser.contentDocument.body.innerHTML)
              }, false);
              mobs.disconnect();
            }
          /*
            if (mutation.attributeName == 'progress' && attrVal == '') {
             //cannot add addEventListener for DOMConentLoaded here as document is already loaded, it will never fire
              alert('tab done loading');
              mobs.disconnect();
            }
           */
        });
    });
    mobs.observe(tab, {
        attributes: true
    });

console.log(tab._fullyOpen)