By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,030 Members | 1,231 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,030 IT Pros & Developers. It's quick & easy.

Scope of Js variable

P: 9
I have a scenario.

File A.vxml

<script src=.".. /test.js">
var abc={};


test.js // No declaration of abc in this js file.

abc['hello']=["Hello"];

If I compile A.vxml, it is not able to refer to the abc variable from the js file.
I do not want to declare the abc variable in the JS file.

Is there any way I can get this thing working?
Feb 24 '09 #1
Share this Question
Share on Google+
14 Replies


Dormilich
Expert Mod 5K+
P: 8,639
I don't understand the problem. If you don't want abc in the JS file and the JS file isn't used during compilation anyway, just leave abc out?
Feb 24 '09 #2

P: 9
Yes may be I didn't write problem clearly.

The JS file contains the variable arrary.. as in ..
abc['LINK']=["Link"];
abc["IMG']=[" Image is clicked"];
....
...........
and this array will be called from various places in the package.

There is this file A.vxml which gets loaded once our project is started .. as in .. you can say that this is the first file which gets loaded into RAM once the project starts. So I am including the JS file in this file ( with use of <src> tag) so that JS file gets loaded too ..because it will be used by many other programs.

Everything is working fine If i declare the abc variable in the JS file itself.
I included the JS file in A.vxml (using src tag) and write just the declaration part in the vxml file. the usage is in the JS file and the declaration is in vxml file.

Now when some part of the program, when calls abc[IMG] is unable to retrieve the information in that entry(The image is clicked).

Does this make sense? I tried to explain the problem .. hope it helps. Kindly provide me with some suggestion.
Feb 24 '09 #3

Dormilich
Expert Mod 5K+
P: 8,639
I think I got it.

well, if you (re)declare abc in the script tag in .vxml, you're effectively overwriting the variable abc in the included JS file (both variables have the same (= global) scope).
Feb 24 '09 #4

P: 9
Thank for the reply :)
But I am not redeclaring it. Just declaring the abc variable in the vxml file.
There is NO declaration in the js file.
Feb 25 '09 #5

Dormilich
Expert Mod 5K+
P: 8,639
if you use abc in the JS file (whether it is abc.link or anything else) you have it declared in the first statement that uses abc.
Feb 26 '09 #6

P: 9
You mean the there is no way I can do it without declaration of abc in the JS File?

On the first place, can we include JS scripts in any format file? as in vxml,java... is JS so robust??
Mar 2 '09 #7

Dormilich
Expert Mod 5K+
P: 8,639
@nileshNP
you should always declare the variables you use. not doing so could be a source of problems and errors (e.g. the variable gets the wrong scope).
@nileshNP
as long as the JS code block is marked as such and the format file allows it, why not? (JS code is text until a user agent that is capable of JS parses it). file format validaty is something different, though.
Mar 2 '09 #8

P: 9
Thank you . That was quick :)
@Dormilich
format file allows it? Here you mean, if vxml allows it? How do I know if a format allows js code or not?
Mar 2 '09 #9

Dormilich
Expert Mod 5K+
P: 8,639
@nileshNP
that's part of the manual/specs. You have to look it up.
Mar 2 '09 #10

P: 9
Hey - I have asked questions in the "quote" block too. Didn't quite knew what a quote block is untill I submitted the query. Please answer them.

I will look up the specs.
Mar 2 '09 #11

Dormilich
Expert Mod 5K+
P: 8,639
@nileshNP
the included JS is treated more like a library, where the (in the vxml file) called functions look up the source code (kind of... simplified)

@nileshNP
is there a good reason for that? I think it's best to declare the variables, where you use them (so the script will work as a whole).

@nileshNP
since it works.... it makes all the difference in the world. (and besides that, it's much easier and cleaner)
Mar 2 '09 #12

P: 9
[quote] @Dormilich
is there a good reason for that? I think it's best to declare the variables, where you use them (so the script will work as a whole).
Well my manager says that in future, we will be going to create many js files as such. Present one is for english language. Many other language's js files to come. I really don't understand how will it make a difference if you declare here or there. May be memory space??

Since it works.... it makes all the difference in the world. (and besides that, it's much easier and cleaner)
Yes it is.

Well need to figure out a way of doing it. Else just continue with my old approach which is simple and as efficient as other.
Mar 2 '09 #13

Dormilich
Expert Mod 5K+
P: 8,639
@nileshNP
not that I'm aware of. but a variable declaration is not a big deal (IMO) and it can save you much trouble declaring it in the JS file (as you said).

besides that it makes debugging easier...
Mar 2 '09 #14

P: 9
Should not be a big deal but I am finding it to be :(
Last try today
Mar 2 '09 #15

Post your reply

Sign in to post your reply or Sign up for a free account.