Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7971305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:36:30+00:00 2026-06-04T07:36:30+00:00

I am building a firefox addon that loads javascript at every page load. I’m

  • 0

I am building a firefox addon that loads javascript at every page load. I’m using progress listener function I found on this page: https://developer.mozilla.org/en/Code_snippets/Progress_Listeners

My problem is that the code seems to execute to early before the page is fully loaded which causes my script to not run. Here is my code.

var PageLoad = {
    browser: null,
    domain:  null,
    oldURL:  null,

    init: function() {
            gBrowser.addProgressListener(urlBarListener,Components.interfaces.nsIWebProgress.NOTIFY_LOCATION);
    },

    uninit: function() {
            gBrowser.removeProgressListener(urlBarListener);
    },

    processNewURL: function(aURI) {
            //if (aURI.spec == this.oldURL)
            //return;

        MyObject.function();

            this.oldURL = aURI.spec;
    }
};

var urlBarListener = {
    locChange: false,

    QueryInterface: function(aIID) {
            if (aIID.equals(Components.interfaces.nsIWebProgressListener) ||
               aIID.equals(Components.interfaces.nsISupportsWeakReference) ||
               aIID.equals(Components.interfaces.nsISupports))
            return this;
            throw Components.results.NS_NOINTERFACE;
    },

    onLocationChange: function(aProgress, aRequest, aURI) {
            PageLoad.processNewURL(aURI);
    },

    onStateChange: function(aWebProgress, aRequest, aFlag, aStatus) {},
    onProgressChange: function(a, b, c, d, e, f) {},
    onStatusChange: function(a, b, c, d) {},
    onSecurityChange: function(a, b, c) {}
};

window.addEventListener("load",
    function() {
            PageLoad.init()
    }, false);

var MyObject = {
    function : function() {
            var script = PageLoad.browser.createElement('script');
            script.src = 'url_to_script.js';
            PageLoad.browser.getElementsByTagName('head')[0].appendChild(script);
    }
};

With this code I get this error message on the console:

PageLoad.browser.getElementByTagName(“head”)[0] is undefined

If I add a timeout then the script does work intermittently but if the page loads slow I get the same error, here’s what works sometimes setTimeout(MyObject.function, 1000);

I need a more reliable way of making sure it’s executing the script after the page is loaded.

Unrelated, and it doesn’t seem to cause any problems but I also see this error message:

gBrowser.addProgressListener was called with a second argument, which is not supported. See bug 608628.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T07:36:32+00:00Added an answer on June 4, 2026 at 7:36 am

    If you want to load javascript at every page load – the best way is subscribing to DOMContentLoaded event:

    var MyObject  = {
        processDOMContentLoaded: function(doc) {
            var script = doc.createElement('script');
            script.src = 'url_to_script.js';
            script.type = 'text/javascript';
            doc.getElementsByTagName('head')[0].appendChild(script);
        }
    };
    
    window.addEventListener('load', function() {
        var appcontent = document.getElementById('appcontent');
        if(appcontent != null) {
            appcontent.addEventListener('DOMContentLoaded', function(event) {
                var doc = event.originalTarget;
                if(doc instanceof HTMLDocument) {
                    MyObject.processDOMContentLoaded(doc);
                }
            }, true);
        }
    }, false);
    

    Have not tested, but should work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a Firefox extension and started building automated tests using jsUnit. I
I am building a Firefox add-on that needs to extract details of SSL certificates
I'm building a Firefox Extension that, among other things, needs to replace body background
I am building a web page that uses slidejs image slider to show a
I am building a firefox addon for the first time. When I try to
I'm building an add-on for FireFox that simulates a website, but running from a
I'm building a little pagination with jquery. I found a plugin that does some
I'm building today's date using a onclick event in JavaScript. When i set the
I am building a simple Firefox extension using the Add-on SDK 1.0. Scouring the
We're building an application that makes extensive use of IndexedDB on Firefox to store

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.