Can a javascript variable be used from a different file -


this question has answer here:

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

Popular posts from this blog

serialization - Convert Any type in scala to Array[Byte] and back -

Slow performance first queries on SQL Azure -

Java Entity Manager - JSON reader was expecting a value but found 'db' -