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

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

mapreduce - Resource manager does not transit to active state from standby -

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