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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:42:30+00:00 2026-05-30T00:42:30+00:00

I am looking for some more information on how browsers execute async style scripts

  • 0

I am looking for some more information on how browsers execute async style scripts with regards to the rest of the page.

I have a JS script that follows the async loading pattern like so:

var s = document.createElement('script');
s.type = 'text/javascript';
s.async = true;
s.src = 'http://yourdomain.com/script.js';
var x = document.getElementsByTagName('script')[0];
x.parentNode.insertBefore(s, x);

I understand that this is a non-blocking download and the page will continue to load while this downloads. So far so good. Now lets say later on I declare some variable:

var x = "something";

My question is, will this variable be available to the script I loaded previously? If I do a

alert(x);

in the script loaded above, it seems to work but I’m not sure it will always be the case. Also what about DOM elements. If there is some div later on in the page:

<div id="myDiv"></div>

and in my script I do

var div = document.getElementById("myDiv"); 

Will this work correctly? In my testing these things seem to work, but I’m not sure if there was much more page content that it would be visible to the async script or not.

So my question can be basically narrowed down to, is it safe to access items from a asynchronously loaded script without checking for some DOM loaded event?

  • 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-30T00:42:32+00:00Added an answer on May 30, 2026 at 12:42 am
    1. By adding a <script> with async set to true and then declaring a variable in a <script> lower down the pecking order, it’s a race condition whether the variable is available in the first.

      If the first <script> is cached, or if the execution of the second <script> is delayed (i.e. an element inbetween the first/ second is synchronous and slow to load) you will end up with the variable being undefined.

    2. You can guarantee that a <script> will execute no earlier than it’s position in the DOM. If the <div> is positioned before the <script>, it will always be available. Otherwise pull straws.

    In both of these situations you can wrap JavaScript code contained within the scripts in event handlers which are listening for an event such as window.onload or DOMContentLoaded event to delay the execution until those events have fired (and all <scripts> will be present and correct).

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

Sidebar

Related Questions

Not a great title but I'm looking more for some guidance, have searched quite
Thanks for all your help, as usual, i am looking for some more information...
I'm looking for some information about the roll-out of a python project that uses
I have been trying to find some more information about the next Microsoft Dynamics
I don't have a specific question here but I'm more looking for some guidance
Looking for some direction here as I'm running into some migration problems. We have
Looking at some code I'm maintaining in System Verilog I see some signals that
Looking for some guidance on a WCF service I’m prototyping. I have a WCF
looking for some help with images referenced within the stylesheet. I have no problems
I'm looking to write an iPhone app that populates a UITable with information from

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.