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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:51:15+00:00 2026-05-16T11:51:15+00:00

today I’ve been working on loading dynamic javascript code (files). The solution I use

  • 0

today I’ve been working on loading dynamic javascript code (files). The solution I use is :

function loadScript(scriptUrl) {
        var head = document.getElementsByTagName("head")[0];
        var script = document.createElement('script');
        script.id = 'uploadScript';
        script.type = 'text/javascript';
        script.src = scriptUrl;
        head.appendChild(script);
    }

the problem with this is that I don’t know how to make sure WHEN the script contents are executed. Since the script contains classes (well JS classes), I wrote down a function that is called through setTimeout and checks if those objects are defined. This is not flexible as it is not automatical. So? Are there ways to load those scripts and have a reliable notification on when they have been executed?

  • 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-16T11:51:16+00:00Added an answer on May 16, 2026 at 11:51 am

    The easiest way short of a JS library is to make an XMLHttpRequest and eval() the return. Your “onload” would be when the data is returned, and “oninit” would be right after you’ve eval’d.

    EDIT: If you want to sequence this, you can create an AssetLoader that takes an array of scripts and won’t eval() a script until the one preceding it has been fetched and initialized.

    EDIT 2: You can also use the script.onload stuff in the post referenced in the comments. The eval method has a slight advantage in that you can separate and control the load and execution portions of the script import.

    EDIT 3: Here’s an example. Consider a file called foo.js that contains the following:

    
    function foo () {
        alert('bar');
    }
    
    

    Then execute the following from another page in your browser:

    
    
    function initScript (scriptString) {
        window.eval(scriptString);
    }
    
    function getScript (url, loadCallback, initCallback, callbackScope) {
    
        var req = new XMLHttpRequest();
        req.open('GET', url);
    
        req.onreadystatechange = function (e) {
    
            if (req.readyState == 4) {
                if (loadCallback) loadCallback.apply(callbackScope);
                initScript.call(null, req.responseText);
                if (initCallback) initCallback.apply(callbackScope);
            }
    
        }
    
        req.send();
    
    }
    
    function fooScriptLoaded () {
        alert('script loaded');
    }
    
    function fooScriptInitialized () {
        alert('script initialized');
        foo();
    }
    
    window.onload = function () {
        getScript('foo.js', fooScriptLoaded, fooScriptInitialized, null);
    }
    
    

    You will see the alerts “script loaded”, “script initialized”, and “bar”. Obviously the implementation of XMLHttpRequest here isn’t complete and there are all sorts of things you can do for whatever scope you want to execute the script in, but this is the core of it.

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

Sidebar

Related Questions

today I've been working with Boost::shared_ptr, and I have a question. vector<shared_ptr<KlasaA> > vec;
Today I had to use the basename() function, and the man 3 basename (
Today I had a coworker suggest I refactor my code to use a label
Today I had a discussion with a colleague about nested functions in Javascript: function
Today, while I was randomly reading the JavaScript patterns O'Reilly book, I found one
Today, someone attended me to bad use of the return keyword in Java. I
Today I tried to use const indentifier, but I find the const variable can
Today I just opened the project I was working on XCODE 4, and only
Today I was reading code from some very popular numerical libraries written in FORTRAN
Today I was working on a tab navigation for a webpage. I tried the

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.