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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:26:52+00:00 2026-05-23T22:26:52+00:00

I created the example below where the page loads the temp.js dynamically at the

  • 0

I created the example below where the page loads the temp.js dynamically at the bottom of the HTML page. The temp.js has a little sleep function which binds up browser for 3 seconds before logging ‘Script Loaded’. At the very bottom of the HTML page it logs a ‘Page Loaded’

Now, knowing what I know about browsers, downloading resources and the single threaded nature of JS I thought this would happen.

  1. HTML is displayed
  2. console.log ‘Page Loaded’
  3. About a three seconds wait whilst temp.js does it stuff
  4. console.log telling me ‘Script Loaded’

but what in fact happens is this

  1. console.log ‘Page Loaded’ (almost instantly)
  2. Blank page for about three seconds
  3. HTML is displayed and console.log telling me ‘Script Loaded’

Is this the behaviour you would have expected?


function sleep (ms) {
  var now = (new Date()).getTime ();
  while ((now + ms) > ((new Date()).getTime ())) {
  }
}
sleep (3000);
console.log ('Script Loaded');

<html lang="en-US">
  <head>
    <meta charset="UTF-8">
    <title>querySelectorAll</title>
  </head>
  <body>
    <ul>
      <li><a href="">One</a></li>
      <li><a href="">Two</a></li>
    </ul>

    <div id="nick">
      <img src="image.png" alt="" width="10" height="10" />
      <img src="image.png" alt="" width="10" height="10" />
      <img src="image.png" alt="" width="10" height="10" />
    </div>
    <!-- <script type="text/javascript" src="temp.js"></script> -->
    <script type="text/javascript">
      (function() {
        var e = document.createElement('script');
        e.src = 'temp.js';
        e.async = true;
        document.getElementsByTagName("head")[0].appendChild(e);
      })();
    </script>
    <script type="text/javascript">
      console.log('Page Loaded');
    </script>
  </body>
</html>
  • 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-23T22:26:54+00:00Added an answer on May 23, 2026 at 10:26 pm

    What happens

    • Some HTML is parsed
    • <script> block 1 is parsed and it adds a new script tag to the <head>
    • <script> block 2 is parsed and it logs page is loaded
    • <script> block in head is parsed and it runs your blocking sleep function
    • 3 seconds pass
    • Script loaded is logged
    • <head> block has finished parsing and it renders the <body>
    • Page is displayed.

    Change

    document.getElementsByTagName("head")[0].appendChild(e);

    To

    document.getElementsByTagName("body")[0].appendChild(e);

    And the HTML page will load first.

    The problem is that all scripts in the <head> have to be loaded and run before the HTML body is rendered

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

Sidebar

Related Questions

Our application has many controls that are created dynamically. For example, a navigation pane
The following is a simplified example of a page a user has created at
How can I get only unique departments from the below example? Dept Id Created
I have a loop created with each, check this example: $('.foo').each(function(i){ //do stuff });
I have created a certificate basically straight from the keytool example page: keytool -genkey
I have two websites, let's say they're example.com and anotherexample.net . On anotherexample.net/page.html ,
In this little example below in PHP what would be a good way to
I have created a web page which will add an Indian rupee symbol to
For example I now created a this tiny class: public static class FileSystemInfoComparers<T> where
Just created an acc on SO to ask this :) Assuming this simplified example:

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.