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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:02:46+00:00 2026-06-10T00:02:46+00:00

I have two external Javascript files. I declared a variable in one file and

  • 0

I have two external Javascript files. I declared a variable in one file and I am trying to access the variable from other. When I try to access it, it returns undefined.

<script src="script1.js"></script>
<script src="script2.js"></script>

script1:

$(function(){

    var myvar=35;
});

script2:

$(function(){

    alert(myvar); //this line causing error undefined.

});
  • 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-10T00:02:47+00:00Added an answer on June 10, 2026 at 12:02 am

    Your variable isn’t global. You’ve declared it inside a function so it is local to that function. You need to move the var statement outside your document ready function:

    var myvar=35;
    
    $(function(){
        // other document ready stuff here, including
        // using or assigning a value to myvar if needed
    });
    

    Then it will be globally scoped and can be accessed from other script files (as long as they’re included after the one where it’s declared).

    If you don’t know the value to assign until the document ready then do this:

    var myvar;       // declare variable
    
    $(function(){
        myvar = 35;  // assign value
    });
    

    Since you don’t try to use the value until the other script’s document ready handler runs this would be fine.

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

Sidebar

Related Questions

I have two external JavaScript lib files I have to load on same JSP
I have two files, in one I declare the variable SEED as such: ;file1.asm
I have two classes (MVC view model) which inherits from one abstract base class.
I have two files client.php and server.php. The client file send a HTTP request
Here is my .classpath file, after I have added two more external jars (org.restlet.ext.simple.jar
I have two SWFs, one is a 960x30px, the other is a 960x300px. I
I have some JavaScript(1.7.2) in an external .js file that's working ok. But after
Have two folders with approx. 150 java property files. In a shell script, how
Have two actionsheet buttons and one modalviewcontroller on mainviewcontroller in application. Now for two
I have two Virtual machines on an external server. VM1 is the Tomcat server

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.