Is it possible to use a variable in a file called first.js
inside another file called second.js
?
first.js
contains a variable called colorcodes
.
Is it possible to use a variable in a file called first.js
inside another file called second.js
?
first.js
contains a variable called colorcodes
.
You can export the variable from first file using export.
Then, import the variable in second file using import.
export - MDN
I came across amplify.js. It's really simple to use. To store a value, let's call it "myValue", you do:
And to access it, you do
I did like what answer above said but although, it didn't worked with me
because I was
declaring
these variablesinside
JQuery$( document ).ready()
If you store your colorcodes in a global variable you should be able to access it from either javascript file.
As Fermin said, a variable in the global scope should be accessible to all scripts loaded after it is declared. You could also use a property of
window
or (in the global scope)this
to get the same effect.... in another file ...
... in your html file ...
This should work - define a global variable in firstfile and access it from secondfile:
firstfile.js:
secondfile.js:
Note that the order in which you load the script files is significant for some browsers (IE6 for sure, maybe others)