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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T00:28:22+00:00 2026-06-05T00:28:22+00:00

I load the same script in my page many times. I have some trouble

  • 0

I load the same script in my page many times. I have some trouble on decide which is loaded first/after in my website, due to the async/load functions.

So, I’d like to put a global variable that count, when the script is loaded, the order of them.

So myScript.js will start with :

(function () {
    var privateNumberScriptLoaded;

    if (numberScriptLoaded === undefined) {
        numberScriptLoaded = 0;
    }
    else {
        numberScriptLoaded = numberScriptLoaded + 1;
    }

    privateNumberScriptLoaded = numberScriptLoaded;
    console.log(privateNumberScriptLoaded);
})();

but when I load it with :

<script src="http://www.mywebsite.com/widget/myScript.js?type=normal" type="text/javascript"></script>
<script src="http://www.mywebsite.com/widget/myScript.js?type=rotation" type="text/javascript"></script>

I get (for two times) numberScriptLoaded is not defined.

How can I resolve this trouble? In fact I’ll “create” a global variable in my website if it doesnt exist. Than increment it and store in a “private” variable for each script, so I can save the order of the execution for each script.

  • 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-06-05T00:28:23+00:00Added an answer on June 5, 2026 at 12:28 am

    At present, your script falls prey to The Horror of Implicit Globals. I’d recommend not doing that.

    You have three options:

    1. As all global variables end up as properties on window, you could use window explicitly:

      if (!window.numberScriptLoaded) {
          window.numberScriptLoaded = 1; // 1, not 0
      }
      else {
          ++window.numberScriptLoaded;
      }
      

      Unlike the code without the window. prefix, that won’t throw a ReferenceError, because looking up a property on an object works differently from resolving a freestanding identifier.

      Live demo | demo page source | source of script it loads

    2. Always put var numberScriptLoaded; (with no initializer) at global scope in your script, e.g. outside your scoping function:

      var numberScriptLoaded; // No initializer (no = 0 or anything)
      

      On the first load, this will create the variable; on subsequent loads, it’s a no-op. Then you can do this without a ReferenceError:

      if (!numberScriptLoaded) {
          numberScriptLoaded = 1; // 1, not 0
      }
      else {
          ++numberScriptLoaded;
      }
      

      Live demo | demo page source | source of script it loads

    3. Use typeof. If you take the typeof a variable that doesn’t exist, you don’t get a ReferenceError; you get "undefined". Then you can create it via the window. prefix (so you’re not falling prey to The Horror).

      if (typeof numberScriptLoaded === "undefined") {
          // Assigning to window.numberScriptLoaded creates the global
          window.numberScriptLoaded = 1; // 1, not 0
      }
      else {
          ++numberScriptLoaded;
      }
      

      Live demo | demo page source | source of script it loads

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

Sidebar

Related Questions

I have many elements on page and all of which i want to translate
I have a page which lists a load of portfolios. I am trying to
Is it the same if you load javascript to the page like this: <script
I want to load defined folders into my website from same system, now I
I have a load of tables all with the same 2 datetime columns (lastModDate,
I have two load balanced servers each running exactly the same copy of various
I have an environment where 2 load-balanced clustered tomcats pointing to same application. When
I have two external JavaScript lib files I have to load on same JSP
When I try to load two of the same page from my server in
I have a problem wtih session_start() on primary server. When I load page for

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.