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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:56:23+00:00 2026-05-17T16:56:23+00:00

What I’m trying to do is dynamically include one or more js files from

  • 0

What I’m trying to do is dynamically include one or more js files from within Javascript. I know there are a bunch of techniques for this, but I’m looking for a solution that follows these rules:

1) Not use any JS frameworks or libraries (jQuery, Prototype, etc).

2) The script should halt execution until the external js file is completely loaded and parsed by the browser.

The rational behind #2 is the external js file will include function definitions that need to be used immediately after the script is included. Most of the time the browser hasn’t had time to parse the js file before I start calling those functions.

I don’t mind using a callback to know when the script is finished loading, but:

1) I don’t know ahead of time how many scripts are going to be dynamically included, so I don’t want and can’t write a bunch of nested callbacks. I just need to know when they’re all finished loading.

2) I’m wording that trying to use some kind of “load” event to fire the callback might not work if the browser has cached the JavaScript.

Edit I should have mentioned from the start that this is for a plugin system, but I wanted to keep my question/answer generic enough to be helpful to other people.

Users would define which plugins they want to load in an array.

plugins = [ 'FooPlugin', 'BarPlugin' ];

I would then loop through the array, and load the js script for each plugin:

for(var i = 0; i < plugins.length; i++) {
    loadScript('plugins/' + plugins[i] + '.js');
}

Each plugin pushes itself onto the loaded_plugins array (This is an example of FooPlugin.js)

load_plugins.push({
    name: 'FooPlugin',
    // Other plugin methods and properties here
});
  • 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-17T16:56:23+00:00Added an answer on May 17, 2026 at 4:56 pm

    Works cross browser, starting with IE6.

    document.loadScript = function (src, callback) {
        function script(src, onload) {
            var scriptTag = document.createElement('script');
            if (onload) scriptTag.onload = onload;
            scriptTag.src = src;
            scriptTag.type = 'text/javascript';
            return scriptTag;
        }
        function outer(tag) { 
            var d = document.createElement('div');
            d.appendChild(tag);
            return d.innerHTML;
        }
        if (!(src instanceof Array)) src = [src];
        var i, scr, 
            callbackId = 'dynCall_'+Math.floor(Math.random()*89999+10000);
            counter = src.length, 
            call = function () { if (--counter == 0) callback(); };
        if (!document.body) {
            window[callbackId] = function () {
                delete window[callbackId];
                if (callback instanceof Function) callback();
            };
            for (i=0; i<src.length; i++) document.write(outer(script(src[i])));
            document.write('<scr'+'ipt type="text/javascript">'+'window.'+callbackId+'();'+'</scr'+'ipt>');
            return;
        }
        for (i=0; i<src.length; i++) document.body.appendChild(script(src[i], call));
    };
    

    Minified / obfuscated:

    (function(){document.loadScript=function(src,callback){function script(src,onload){var scriptTag=document.createElement('script');if(onload)scriptTag.onload=onload;scriptTag.src=src;scriptTag.type='text/javascript';return scriptTag}function outer(tag){var d=document.createElement('div');d.appendChild(tag);return d.innerHTML}if(!(src instanceof Array))src=[src];var i,scr,callbackId='dynCall_'+Math.floor(Math.random()*89999+10000);counter=src.length,call=function(){if(--counter==0)callback()};if(!document.body){window[callbackId]=function(){delete window[callbackId];if(callback instanceof Function)callback()};for(i=0;i<src.length;i++)document.write(outer(script(src[i])));document.write('<scr'+'ipt type="text/javascript">'+'window.'+callbackId+'();'+'</scr'+'ipt>');return}for(i=0;i<src.length;i++)document.body.appendChild(script(src[i],call))};document.loadScript.toString=function(){return'function loadScript() { [obfuscated] }'}})();
    

    Quick explanation of the branches:

    If the script tag calling loadScript is within the document head or body, document.body will be undefined, as the DOM isn’t in yet. As such, we can’t use the standards method of appending a tag, and we must use doc write. This has the advantage that the tags we write will happen in necessarily sequential order, but the disadvantage that we must have a global scope callback function.

    Meanwhile, if we have document.body, we’re golden for Doing It Right (-ish – we make sacrifices when there’s no libraries around to help do it right-er, hence the .onload(). Still, it’s not like we’re about to throw lots of events on a script tag, so it’ll probably be OK.) The disadvantage (maybe?) is that the scripts all load asynchronously, so we need to have a countdown run as they load.

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

Sidebar

Related Questions

No related questions found

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.