Search code examples
javascriptfirefoxevent-handlingfirefox-addondom-events

How to find out if DOMContentLoaded event comes from iframe or not?


I am working on a Firefox extension and it uses something like this:

function myExt()
{
    this.handleEvent = function (event)
    {
        switch (event.type)
        {
            case "DOMContentLoaded":
            {
                alert('fired');
            }
        }
    }

    window.addEventListener ("DOMContentLoaded", this, false);
}

My problem is that the alert gets executed multiple times if the page contents iframes, so what I am looking to do is, using "event" on this.handleEvent I need to find out if event.target references the top window or the iframe window.

How can I do this?


Solution

  • I am still doing some tests but it looks like this function does the job:

    this.isTopLevel = function(event){
        var doc = event.target;
        for(var i = 0; i < gBrowser.browsers.length; i++) {
            if(gBrowser.browsers[i].contentDocument == doc) return true;
        }
        return false;
    };