BrodaTherapy
BrodaTherapy

Reputation: 91

Sharing variables between .js files in separate folders/locations

I know this may be a simple question but it has me stumped. How do about sharing a global variable between two .js files that are in different folders? Any help would be appreciated. Thanks

Upvotes: 2

Views: 955

Answers (1)

Ben Rhys-Lewis
Ben Rhys-Lewis

Reputation: 3246

Call both files making sure that call your file where you define it first. So the first js file ill define and initialize the variable which you can then call in the second.

EXAMPLE: Call the two files in your html page head

<script type="text/javascript" src="variable.js"></script>
<script type="text/javascript" src="folder/main.js"></script>

So you define the variable in variable.js like:

var VAR = {
    value: "Test"
 };

And then you can use it in main.js.

document.write(VAR.value);

Upvotes: 4

Related Questions