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

  • Home
  • SEARCH
  • 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 6917845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:46:54+00:00 2026-05-27T09:46:54+00:00

I listen for a onload event on a main window, then for a DOMContentLoaded

  • 0

I listen for a onload event on a main window, then for a DOMContentLoaded event on each browser/tab (I need run the code inside a browser at the most) and flag with controlExecuted that the code was run on a browser.

window.addEventListener("load", function(){
    try{
        var controlExecuted = 0;
        var appcontent = document.getElementById("appcontent"); // browser
        if (appcontent && controlExecuted == 0)
            appcontent.addEventListener("DOMContentLoaded", function docLoader(e){
                appcontent.removeEventListener("DOMContentLoaded", docLoader, false);
                alert('run code')
                controlExecuted = 1;
            }, true);
    }catch(e){
        // log!
    }
}, false);

This not works. The code run several times depending of the number of browsers/tabs.

So how to run only one instance of code in a firefox addon?

  • 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-05-27T09:46:55+00:00Added an answer on May 27, 2026 at 9:46 am

    Here the complete solution to run only one instance of code in a firefox addon using JavaScript code modules.

    // modules/main.jsm
    var EXPORTED_SYMBOLS = ["instancesControl"];
    
    var instancesControl = {
        executed : 0,
        windowExecuting : 0,
    
        get : function(){
            return ++instancesControl.executed;
        },
        reset : function(){
            instancesControl.executed = 0;
        },
        getWindowExecuting : function(){
            return instancesControl.windowExecuting;
        },
        setWindowExecuting : function(id){
            instancesControl.windowExecuting = id;
        }   
    };
    

    The main calling code:

     // load instancesControl module
    Components.utils.import("resource://my-own-alias/main.jsm");
    
    // identify window that will execute the one-instance code
    var util = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
    .getInterface(Components.interfaces.nsIDOMWindowUtils);
    var windowID = util.outerWindowID;
    
    // listen load window event 
    window.addEventListener("load", function(){
        var appcontent = document.getElementById("appcontent");
        if (appcontent){
            // listen load content (on every tab)
            appcontent.addEventListener("DOMContentLoaded", function docLoader(e){
                appcontent.removeEventListener("DOMContentLoaded", docLoader, false);
                if (instancesControl.get() == 1){
                    instancesControl.setWindowExecuting(windowID);
    
                    // this code will run only once no matter
                    // how many tabs or windows user opens.
    
                }
            }, true);
        }
    }, false);
    
    // re-init code if executing windows is closed
    window.addEventListener("unload", function(){
        if (instancesControl.getWindowExecuting() == windowID){
            instancesControl.reset();
        }
    }, false);
    

    Sure that you can improve it. Comment using @nomikos if you do.
    THX.

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

Sidebar

Related Questions

Have following listener for keyboard ArrowDown event(it's key code is 40 ): window.onload =
Is there anyway to listen to the onload event for a <link> element? F.ex:
I am reading someone Else's javascript code which is listed below window.onload = function(){
I would like to listen for the mouse over event in GWT 1.6. Since
Say if I listen for an event: Subject.NewEvent += delegate(object sender, NewEventArgs e) {
I am trying to listen tab-in tab-out action for my swing gui that is
How do I listen to click event on a ListView? This is what I
I need to listen for user log in and log out events in Liferay.
I am need to listen to all the serial ports in my machine. Say
I want to listen on an event (clipboard event ?) in order to alter

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.