/    Sign up×
Community /Pin to ProfileBookmark

JavaScript variables

Hi,

Can someone please tell me how to access JS variables outside the file.
i.e. i want to access JS variables from one file in some other file.
like some super global stuff.

Thanks!

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@rnd_meJan 26.2010 — files have no scope, therefore:

  • 1. declare the shared variable in the main program of the .js file, that is to say, "not in a function".


  • 2. refer to variable by name from anywhere.



  • 3. you can also create globals from within a function by omitting "var " before "declaring" them:

    file1.js
    [CODE]function set1(){ X = true; }[/CODE]


  • file2.js:
    [CODE]alert( typeof X );
    set1();
    alert( X ); [/CODE]

    which should show "undefined", then "true" so long as file1 is loaded before file2...
    Copy linkTweet thisAlerts:
    @MrEnderJan 26.2010 — load both files into your html then u can call on variables from any file.

    I often have one JS library for just variables
    ×

    Success!

    Help @dev_d spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.7,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...