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, includ开发者_运维百科ing 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.
精彩评论