简体   繁体   English

如何确定事件来自选项卡的主文档

[英]How to determine the event came from the main document of the tab

I'm developing an extension for Firefox. 我正在为Firefox开发扩展程序。 The extension adds event listener to "appcontent" element on "load" event. 该扩展将事件侦听器添加到“加载”事件的“ appcontent”元素中。

How to determine the event came from the main document of the tab? 如何确定事件来自选项卡的主文档? At the moment all events from different elements of the page come (for example image and even the extension document if it fires one). 此刻,来自页面不同元素的所有事件都来了(例如图像,甚至触发了扩展文档,甚至扩展文档)。 I would like to exclude all the cases, including frames, iframe and so on, only the url typed in the location bar. 我想排除所有情况,包括框架,iframe等,仅排除在位置栏中键入的url。

Just an answer for those who gave points to the question itself and who might find the question through the search. 对于那些指出问题本身并可能通过搜索找到问题的人来说,这只是一个答案。

The task is solved with the line 用线解决任务

  if (Event.originalTarget == content.document)

worked for me. 为我工作。

Found in some newsgroup 在某些新闻组中找到

Can you compare event.srcElement.ownerDocument the main page document? 您可以比较event.srcElement.ownerDocument主页文件吗? You could also use the .location.href properties. 您也可以使用.location.href属性。 Quick and dirty example: 快速而肮脏的例子:

//- on event
var doc = event.srcElement.ownerDocument;
if (doc && (doc.location.href == currentUrl))
    runFunction();

https://developer.mozilla.org/En/DOM/Node.ownerDocument https://developer.mozilla.org/En/DOM/Node.ownerDocument

Have a look at originalTarget and explicitOriginalTarget attributes of event object. 看一下event对象的originalTargetexplicitOriginalTarget属性。 https://developer.mozilla.org/en/DOM/event.originalTarget https://developer.mozilla.org/en/DOM/event.originalTarget

Use it as something like: 像这样使用它:

if(event.explicitOriginalTarget == theHookedObject) {    
   // do your stuff 
}

Where theHookedObject is the object to which you've attached your listener to. theHookedObject是您已将侦听器附加到的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM