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 7685377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:14:15+00:00 2026-05-31T19:14:15+00:00

I have made a namespaces framework for javascript. I am loading some plugins (.js

  • 0

I have made a namespaces framework for javascript. I am loading some plugins (.js files) which are dynamically added to the HTML.

I am going to try to simplify the code.

This function is used to dinamically load a JS. The callback function is called after .js file has been loaded. Consider that the following code has already been run.

MYNAMESPACE.plugins = ["plugin1", "plugin2"];
MYNAMESPACE.getJS = {
    get: function (url, callback) {
        var script = document.createElement("script");
        var head = document.getElementsByTagName('head')[0];
        script.type = "text/javascript";
        script.src = url;
        head.insertBefore(script, head.firstChild)
        script.onload = callback;
        script.onreadystate = callback;
        return script;
        }
};

I have a init function that loads the plugins contained in MYNAMESPACE.plugins as follows:

MYNAMESPACE.init = function (callback) {
    for (index in MYNAMESPACE.plugins) {
        plugin = MYNAMESPACE.plugins[index];
        MYNAMESPACE.getJS.get(plugin + '.js', function ()
         {
              // This callback is executed when the js file is loaded
         });
    }
    // Here I want to execute callback function, but after all the callbacks in the for loop have been executed.  Something like: if (alljsloaded) callback();
}

In my HTML I have the following script tag:

<html>
    <head>
    <script type="text/javascript">
        $(document).ready(function () {
            MYNAMESPACE.init(); 

            // The following line is not executed correctlybecause init finished before the scripts are loaded and the functionOnPlugin1 is undefined.
            MYNAMESPACE.functionOnPlugin1();
        });
    </script>
    </head>
    <body>
    </body>
</html>

And I want to change it for something like this:

<html>
    <head>
    <script type="text/javascript">
        $(document).ready(function () {
            MYNAMESPACE.init(function() { MYNAMESPACE.functionOnPlugin1(); });  
        });
    </script>
    </head>
    <body>
    </body>
</html>

But I don’t know how to modify the function MYNAMESPACE.init() so it executes the callback after ALL the plugin scripts are loaded.

Any ideas? Maybe using closures.

  • 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-31T19:14:16+00:00Added an answer on May 31, 2026 at 7:14 pm
    1. Use for(;;) to loop through an array, not for(.. in ..).
    2. Create a counter in the function, increment it every time that a script has loaded. When it has reached the number of scripts, invoke the callback function.

    Code:

    MYNAMESPACE.init = function (callback) {
        var allPluginsLength = MYNAMESPACE.plugins.length;
        var loadedCount = 0;
        if (allPluginsLength === 0) callback(); // No plugins, invoke callback.
        else for (var index=0; index<allPluginsLength; i++) {
            plugin = MYNAMESPACE.plugins[index];
            MYNAMESPACE.getJS.get(plugin + '.js', function () {
                  // This callback is executed when the js file is loaded
                  if (++loadedCount == allPluginsLength) {
                      callback();
                  }
             });
        }
    }
    

    Also, change your getJS.get method. Currently, callback always executes when a state changes, which is not desirable. To prevent the callback from being executed twice, the following can be used:

        script.onload = function() {
            callback && callback();
            callback = false;
        };
        // onreadystatechange instead of onreadystate, btw.
        script.onreadystatechange = function() {
            if (this.readyState == 'complete') {
                callback && callback();
                callback = false;
            }
        };
    
    • callback && callback() is used, to ensure that a callback exists. If it does, it’s then invoked.
    • After invoking the callback, it’s immediately removed (callback = null).
      This is necessary, because the onload and onreadystatechange events can fire when the script has loaded. Without my suggested code, the callback will be called multiple times for a single script.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have made a multiplayer game using the GameKit Framework where 2 iPhones/iPods can
I have made a game which is nearly identical to Popcap's Atomica. ( http://www.popcap.com/gamepopup.php?theGame=atomica
I have made a basic ascx control which is just a panel with a
I have made an application using Java/Hibernate/MySQL, but whenever I try running it, it
I have made some research on Stackoverflow about reverse for loops in C++ that
I have made an application (for my self) for feeds reading, using SyndicationFeed ,
I have made a kind of Video Slider, where thumbnail of videos are displaying
I have made a generic Observer interface and an Observable class, but can't compile
I have made a custom formwizard and incorporated it into my admin interface. Basically
I have made a java project and want to deliver it to a client

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.