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

cron - how to properly run Python script with crontab on every system startup -

elasticsearch - Is the order of operations guaranteed in a bulk update? -

Slow performance first queries on SQL Azure -