views:

69

answers:

3

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.

A: 

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

Andy E
A: 

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.

xk0der
+1  A: 

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

Maksee
Note, that this restricts you to the currently visible page. I.e. if a background page finishes loading, it will not pass this check.
Nickolay
Nickolay