简体   繁体   中英

How to determine the event came from the main document of the tab

I'm developing an extension for Firefox. The extension adds event listener to "appcontent" element on "load" event.

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.

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? You could also use the .location.href properties. 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

Have a look at originalTarget and explicitOriginalTarget attributes of event object. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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