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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:39:26+00:00 2026-05-15T00:39:26+00:00

I want to run a bit of javascript before the whole page has loaded.

  • 0

I want to run a bit of javascript before the whole page has loaded. Is this possible? Or does the code start to execute on </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-15T00:39:26+00:00Added an answer on May 15, 2026 at 12:39 am

    Not only can you, but you have to make a special effort not to if you don’t want to. 🙂

    When the browser encounters a classic script tag when parsing the HTML, it stops parsing and hands over to the JavaScript interpreter, which runs the script. The parser doesn’t continue until the script execution is complete (because the script might do document.write calls to output markup that the parser should handle).

    That’s the default behavior, but you have a few options for delaying script execution:

    1. Use JavaScript modules. A type="module" script is deferred until the HTML has been fully parsed and the initial DOM created. This isn’t the primary reason to use modules, but it’s one of the reasons:

      <script type="module" src="./my-code.js"></script>
      <!-- Or -->
      <script type="module">
      // Your code here
      </script>
      

      The code will be fetched (if it’s separate) and parsed in parallel with the HTML parsing, but won’t be run until the HTML parsing is done. (If your module code is inline rather than in its own file, it is also deferred until HTML parsing is complete.)

      This wasn’t available when I first wrote this answer in 2010, but here in 2020, all major modern browsers support modules natively, and if you need to support older browsers, you can use bundlers like Webpack and Rollup.js.

    2. Use the defer attribute on a classic script tag:

      <script defer src="./my-code.js"></script>
      

      As with the module, the code in my-code.js will be fetched and parsed in parallel with the HTML parsing, but won’t be run until the HTML parsing is done. But, defer doesn’t work with inline script content, only with external files referenced via src.

    3. I don’t think it’s what you want, but you can use the async attribute to tell the browser to fetch the JavaScript code in parallel with the HTML parsing, but then run it as soon as possible, even if the HTML parsing isn’t complete. You can put it on a type="module" tag, or use it instead of defer on a classic script tag.

    4. Put the script tag at the end of the document, just prior to the closing </body> tag:

      <!doctype html>
      <html>
      <!-- ... -->
      <body>
      <!-- The document's HTML goes here -->
      <script type="module" src="./my-code.js"></script><!-- Or inline script -->
      </body>
      </html>
      

      That way, even though the code is run as soon as its encountered, all of the elements defined by the HTML above it exist and are ready to be used.

      It used to be that this caused an additional delay on some browsers because they wouldn’t start fetching the code until the script tag was encountered, but modern browsers scan ahead and start prefetching. Still, this is very much the third choice at this point, both modules and defer are better options.

    The spec has a useful diagram showing a raw script tag, defer, async, type="module", and type="module" async and the timing of when the JavaScript code is fetched and run:

    enter image description here

    Here’s an example of the default behavior, a raw script tag:

    .found {
        color: green;
    }
    <p>Paragraph 1</p>
    <script>
        if (typeof NodeList !== "undefined" && !NodeList.prototype.forEach) {
            NodeList.prototype.forEach = Array.prototype.forEach;
        }
        document.querySelectorAll("p").forEach(p => {
            p.classList.add("found");
        });
    </script>
    <p>Paragraph 2</p>

    (See my answer here for details around that NodeList code.)

    When you run that, you see “Paragraph 1” in green but “Paragraph 2” is black, because the script ran synchronously with the HTML parsing, and so it only found the first paragraph, not the second.

    In contrast, here’s a type="module" script:

    .found {
        color: green;
    }
    <p>Paragraph 1</p>
    <script type="module">
        document.querySelectorAll("p").forEach(p => {
            p.classList.add("found");
        });
    </script>
    <p>Paragraph 2</p>

    Notice how they’re both green now; the code didn’t run until HTML parsing was complete. That would also be true with a defer script with external content (but not inline content).

    (There was no need for the NodeList check there because any modern browser supporting modules already has forEach on NodeList.)

    In this modern world, there’s no real value to the DOMContentLoaded event of the “ready” feature that PrototypeJS, jQuery, ExtJS, Dojo, and most others provided back in the day (and still provide); just use modules or defer. Even back in the day, there wasn’t much reason for using them (and they were often used incorrectly, holding up page presentation while the entire jQuery library was loaded because the script was in the head instead of after the document), something some developers at Google flagged up early on. This was also part of the reason for the YUI recommendation to put scripts at the end of the body, again back in the day.

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

Sidebar

Related Questions

Why this code don't work,when i want run this code vwd 2008 express show
I want to run some JS code when an image's CSS property display has
Traditionally, to call a JavaScript function once the page has loaded, you'd add an
I have read and re-read this page, as well as run the samples: http://code.google.com/chrome/extensions/background_pages.html
We have created an application which we want to run in a 64 bit
I want to make existing .NET applications (WinForms and WebForms) run on 64-bit machines,
I want to run a weekly batch process in an asp.net page. How can
I want to run javascript/Python/Ruby inside my application. I have an application that creates
I want to run a bit of Java in Scala console. Here's what I
I've written a bit of code in JavaScript that uses the window.navigator.language and 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.