/    Sign up×
Community /Pin to ProfileBookmark

Question about the scope and availability of vars

[B]Question[/B]: if I declare a variable in on js fiile, is this available to another js file that is loaded?

[B]Answer[/B]: yes, it’s available (unless inside of a function or something)

[B]BUT[/B]: does it matter WHERE I declare this variable??

Let’s say, I have the following in my HTML file:

[INDENT]<script src=”script/main.js”></script>[/INDENT]

And then at the very bottom of my HTML page before the </body> tag I have:

[INDENT]<script type=”text/javascript”>var myvariable = “Sample Data”;</script>[/INDENT]

So, is [B]myvariable[/B] available to main.js?

In simple tests I’ve done, yes it does seem to be available

But… what if I have a lot of other js code running in between
Does this change anything?

Thanks

OM

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@Jeff_MottMay 06.2012 — Let's say, I have the following in my HTML file:

[INDENT]<script src="script/main.js"></script>[/INDENT]

And then at the very bottom of my HTML page before the </body> tag I have:

[INDENT]<script type="text/javascript">var myvariable = "Sample Data";</script>[/INDENT]

So, is [B]myvariable[/B] available to main.js?

In simple tests I've done, yes it does seem to be available[/QUOTE]


If you're setting the variable [i]after[/i] loading main.js, then no, it won't be immediately available. If it's working in your tests, I'm guessing your code waits for the DOM ready event before running.


But... what if I have a lot of other js code running in between

Does this change anything?[/QUOTE]


There's always the possibility that one of the scripts will overwrite your global variable. That's one of the problems with global variables.
Copy linkTweet thisAlerts:
@OM2authorMay 06.2012 — If you're setting the variable [i]after[/i] loading main.js, then no, it won't be immediately available. If it's working in your tests, I'm guessing your code waits for the DOM ready event before running.




There's always the possibility that one of the scripts will overwrite your global variable. That's one of the problems with global variables.[/QUOTE]


jeff, thanks for the reply

that's actually an amazing help

yes: someone else said to me that the dom waits for the ready event

the application is actually for jquerymobile

there'll be no chance of the code being over written - it's just got a unique name and a one off usage

[b]question[/b]: let's say there is a lot of other js code that is loaded and run - is there ANY danger that the code might get executed BEFORE the dom loads? this is my concern

thanks
Copy linkTweet thisAlerts:
@PadonakMay 06.2012 — depends on what is in the code
×

Success!

Help @OM2 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...