Can a javascript variable be used from a different file -
this question has answer here:
- how declare global variable in .js file 5 answers
i making game , wanted know how use variable different file. ex:
file 1:
var js = "js";
file 2:
alert(js);
i know seems kind of weird have reason doing it.
can javascript variable used different file?
yes, can... as long it's global variable.
this because of javascript files loaded shared global namespace.
but warned...
in html, need include script declares variable first. otherwise complain variable undefined.
example
script1.js
var globalnumber = 10;
script2.js
alert(globalnumber); // alert "10"
index.html
<script src="script1.js"></script> <script src="script2.js"></script>
Comments
Post a Comment