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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:09:30+00:00 2026-06-13T23:09:30+00:00

I want to append several js files dynamically. Each file has a function definition

  • 0

I want to append several js files dynamically. Each file has a function definition and I want to save its code in the property of an object. I use plain JavaScript and script tag. But onload event doesn’t work right. If I include just one file everything works fine. But if more than one, onload is out of order. Here’s my code, it is method in the object:

// My files I want to include:
one.js
   function one() { alert('one') };

two.js
   function two() { alert('two') };

// My code in the object 
var one = null;
var two = null;   // In these properties I want to save my functions

function inc(files) {   // 'files' is the object {one: 'one', two: 'two'}
                        'one' and 'two' are names of function in my files
    for (key in files) {

       var script = document.createElement('script');
       script.setAttribute('type', 'text/javascript');

       var fullPath = '/path/to/file/' + files[key] + '.js';   // /path/to/file/one.js, /path/to/file/two.js,
       script.setAttribute('src', fullPath);


       // And here's my eventhandler. After the script is loaded,
       // names of my functions are in global scope. So, the task is:
       // to take them and save in properties using setter.
       script.onload = function() {

           setProps(window[files[key]]);   // setProp(window.one / window.two);
           window[files[key]] = null;      // remove them from global scope

           alert(one/two);   // to check if I have functions saved in properties
       }

       document.getElementsByTagName('head').item(0).appendChild(script);

    };
};

As I said at the beginning, everything works fine if I load one file. But for more than one the onload works twice but only for my second file – with function 'two' definition. Alert fires twice and shows the code of 'two'. And I have only function 'two' im my property 'two', the last one I passed with parameter. And files are appended in DOM just file.

I’ve tried to create <script> tag outside of for/in loop, and even created an array and saved each of two scripts as separate elements of that array but it doesn’t help either – only the last file is in the onload and twice.

What is the problem? Why doesn’t it work for two files? Is it possible to solve it?

  • 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-13T23:09:31+00:00Added an answer on June 13, 2026 at 11:09 pm

    It’s hard to tell when you don’t include complete code, but it’s likely that you’re running into a closure problem.

    You have your onload function defined inside the for loop, which means it’ll bind a reference to the “key” local variable. By the time the “unload” function actually runs, the for loop will have been completed, and “key” will be “two”, so you’ll see “two” in both alerts.

    Here’s a more-trivial example of the same problem:

    for (x=0; x< 2; x++) {
       setTimeout(function() {
           alert(x);
       }, 100);
    }
    

    when you run this, you’ll get two alerts, each of which shows “2”, which is the value of “x” after the loop exits.

    To fix this, you need to invoke a function which returns the function you want to call when the timeout happens:

    function create_alerter(z) {
        return function() {
            alert("fixed: " + z);
        }
    }
    
    for (x = 0; x < 2; x++) {
        setTimeout(create_alerter(x), 100);
    }
    

    If you don’t like having a separate function for this, you can define the function-returning function and invoke it inline:

    for (x = 0; x < 2; x++) {
        setTimeout(function(z) {
           return function() {
              alert(z);
           }
        }(x), 100);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I want to append the contents of a src file into the end
I have a binary file to which I want to append a chunk of
I have defined an object that has several attribute.. class thing(object): def __init__(self, type,
My C# code is generating several text files based on input and saving those
I want to add my C++ source code to the corresponding elf binary file
I want to sum up several vectors of different size in an array. Each
I want to append several strings in a data set with custom strings. Example
I want to append lines to my file. I am using a StreamWriter: StreamWriter
I have a several strings, which i want to append to use it as
I have several resources which I want to model with RDF, each of them

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.