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
Asked
Active
Viewed 779 times
2
-
Possible duplicate of [Call variables from one javascript file to another](http://stackoverflow.com/questions/8351265/call-variables-from-one-javascript-file-to-another) – Ben Rhys-Lewis Feb 19 '16 at 08:42
-
that works if they are in the same diectory. But my problem comes from them being in separate folders – BrodaTherapy Feb 19 '16 at 08:44
1 Answers
4
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);

Ben Rhys-Lewis
- 3,118
- 8
- 34
- 45