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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:51:48+00:00 2026-05-11T19:51:48+00:00

I need a few global variables that I need in all .js files. For

  • 0

I need a few global variables that I need in all .js files.

For example, consider the following 4 files:

  1. global.js
  2. js1.js
  3. js2.js
  4. js3.js

Is there a way that I can declare 3 global variables in global.js and access them in any of the other 3 .js files considering I load all the above 4 files into a HTML document?

Can someone please tell me if this is possible or is there a work around to achieve this?

  • 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-11T19:51:48+00:00Added an answer on May 11, 2026 at 7:51 pm

    Just define your variables in global.js outside a function scope:

    // global.js
    var global1 = "I'm a global!";
    var global2 = "So am I!";
    
    // other js-file
    function testGlobal () {
        alert(global1);
    }
    

    To make sure that this works you have to include/link to global.js before you try to access any variables defined in that file:

    <html>
        <head>
            <!-- Include global.js first -->
            <script src="/YOUR_PATH/global.js" type="text/javascript"></script>
            <!-- Now we can reference variables, objects, functions etc. 
                 defined in global.js -->
            <script src="/YOUR_PATH/otherJsFile.js" type="text/javascript"></script>
        </head>
        [...]
    </html>
    

    You could, of course, link in the script tags just before the closing <body>-tag if you do not want the load of js-files to interrupt the initial page load.

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

Sidebar

Related Questions

I have read in many posts that global variables are bad, but I need
I need several global pointers to be shared among a few files - the
I have a few data values that I need to store on my rails
So I'm finding that I'm using a singleton User class to access global variables
I need to import all our variables from the unnamed Perl module (Module.pm) and
I have a few reasons that I need this to happen, but suffice it
i need to write few applications about lowlevel videocard controling for my coursework. For
I need to initialize few objects, it can take a while so I want
I have a Java servlet from which I need to send few notifications to
I need to impl a few basic features in this server: display a nice

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.