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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:33:31+00:00 2026-05-13T05:33:31+00:00

I’m having some issues with the ordering of javascript execution in the following page:

  • 0

I’m having some issues with the ordering of javascript execution in the following page:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>

<script type="text/javascript">
    var blah = 0;

    function scriptDoneRunning()
    {
        alert(blah);
    }

    function addExternalScript(src, parentNode)
    {
        var sc = document.createElement('script');
        sc.setAttribute('type', 'text/javascript');
        sc.setAttribute('src', src);
        parentNode.appendChild(sc);
    }

    function addEndingScript(parentNode)
    {
        var theDiv = document.createElement('div');
        var sc = document.createElement('script');
        sc.setAttribute('type', 'text/javascript');
        sc.text = "scriptDoneRunning();";
        theDiv.appendChild(sc);
        parentNode.appendChild(theDiv);
    }
</script>
</head>

<body>
<div id="theparent">
    &nbsp;
</div>

<script type="text/javascript">
    addExternalScript("blah.js", document.getElementById("theparent"));
    addEndingScript(document.getElementById("theparent"));
</script>
</body>
</html>

And the blah.js file looks like this….

blah=3;

Essentially the code calls “addExternalScript()” which adds a script element for “blah.js” to the “theparent” element. “blah.js” sets the variable blah to 3.

Then we call “addEndingScript()” which appends a script block to “theparent”. All that script block does is call scriptDoneRunning() which alerts the variable blah.

The idea is that scriptDoneRunning() only gets called AFTER the blah.js has finished executing. Because it is appended to “theparent” after the external script element I would expect it to wait for that to complete then it would call scriptDoneRunning(). This is the behavior I expected, however it only works that way in firefox. In IE the value alerted is 0, which means the code in blah.js has not executed yet.

This is a slimmed down version of what the page actually does so obviously this doesn’t make a ton of sense. But is there a way to append the inline script in such a way that the code in blah.js always executes first in both browsers?

Also, when this is deployed I will not have control over the external js. So keep that in mind 😉

Thanks!

  • 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-13T05:33:31+00:00Added an answer on May 13, 2026 at 5:33 am

    The script loads asynchronously, you can know exactly when the external script has been loaded,using the load (and readystatechange for IE) events, but you should take care of removing the script elements and nullify the handlers after the load is done to avoid known memory leaks.

    I would also recommend you to insert your script elements on the head, there is no really a reason (unless you use document.write which I don’t think is a good idea nowdays) to add the script elements anywhere inside the body…

    I use the following library independent function, inspired by the jQuery’s $.getScript method:

    loadScript("blah.js", function () {
      addEndingScript(document.getElementById("theparent"));
      // or why not simply scriptDoneRunning(); ???
    });
    
    function loadScript(url, callback) {
      var head = document.getElementsByTagName("head")[0],
          script = document.createElement("script"),
          done = false;
    
      script.src = url;
    
      // Attach event handlers for all browsers
      script.onload = script.onreadystatechange = function(){
        if ( !done && (!this.readyState ||
          this.readyState == "loaded" || this.readyState == "complete") ) {
          done = true;
          callback(); // execute callback function
    
          // Prevent memory leaks in IE
          script.onload = script.onreadystatechange = null;
          head.removeChild( script );
        }
      };
      head.appendChild(script);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 290k
  • Answers 290k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It's not a responsibility of a controller, it violates SRP.… May 13, 2026 at 5:50 pm
  • Editorial Team
    Editorial Team added an answer Do it the same way you do for photos. Checkout… May 13, 2026 at 5:50 pm
  • Editorial Team
    Editorial Team added an answer The API you are looking for is called reference transactions.… May 13, 2026 at 5:50 pm

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want use html5's new tag to play a wav file (currently only supported
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I've got a string that has curly quotes in it. I'd like to replace
In order to apply a triggered animation to all ToolTip s in my app,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.