473,387 Members | 1,465 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Scope of Js variable

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
14 2091
Dormilich
8,658 Expert Mod 8TB
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
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
8,658 Expert Mod 8TB
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
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
8,658 Expert Mod 8TB
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
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
8,658 Expert Mod 8TB
@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
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
8,658 Expert Mod 8TB
@nileshNP
that's part of the manual/specs. You have to look it up.
Mar 2 '09 #10
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
8,658 Expert Mod 8TB
@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
[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
8,658 Expert Mod 8TB
@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
Should not be a big deal but I am finding it to be :(
Last try today
Mar 2 '09 #15

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

Similar topics

6
by: pembed2003 | last post by:
Hi all, I am reading the book "C++ How to Program" and in the chapter where it discuss scope rule, it says there are four scopes for a variable: function scope file scope block scope...
3
by: Grant Wagner | last post by:
Given the following working code: function attributes() { var attr1 = arguments || '_'; var attr2 = arguments || '_'; return ( function (el1, el2) { var value1 = el1 + el1; var value2 = el2...
8
by: lawrence | last post by:
I'm learning Javascript. I downloaded a script for study. Please tell me how the variable "loop" can have scope in the first function when it is altered in the second function? It is not defined...
8
by: TTroy | last post by:
I have a few questions about "scope" and "visibility," which seem like two different things. To me "visibility" of the name of a function or object is the actual code that can use it in an...
4
by: Gery D. Dorazio | last post by:
Gurus, If a static variable is defined in a class what is the scope of the variable resolved to for it to remain 'static'? For instance, lets say I create a class library assembly that is...
165
by: Dieter | last post by:
Hi. In the snippet of code below, I'm trying to understand why when the struct dirent ** namelist is declared with "file" scope, I don't have a problem freeing the allocated memory. But...
78
by: Josiah Manson | last post by:
I found that I was repeating the same couple of lines over and over in a function and decided to split those lines into a nested function after copying one too many minor changes all over. The only...
1
pbmods
by: pbmods | last post by:
VARIABLE SCOPE IN JAVASCRIPT LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN ) PREREQS: VARIABLES First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause...
0
MMcCarthy
by: MMcCarthy | last post by:
We often get questions on this site that refer to the scope of variables and where and how they are declared. This tutorial is intended to cover the basics of variable scope in VBA for MS Access. For...
5
by: somenath | last post by:
Hi All , I have one question regarding scope and lifetime of variable. #include <stdio.h> int main(int argc, char *argv) { int *intp = NULL; char *sptr = NULL;
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.