javascriptfirefoxdom-eventsaddeventlistenerxul

Firefox extension. DOMContentLoaded fires more than once on some pages


window.addEventListener('load', function () {
gBrowser.addEventListener('DOMContentLoaded', function () {
    // stuff that happens for each web page load goes here
}, false),false)

I am having the following problem with this code: DOMContentLoaded fires more than once on some pages.


Solution

  • DOMContentLoaded will be called for iframes as well. If you want to ignore these calls, you can do something like this:

    function onLoaded(event) {
      var isFrame = (event.target instanceof Ci.nsIDOMHTMLDocument &&
        event.target != browser.contentDocument);
      if (isFrame) {
        return;
      }
    }