Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Enumerating user defined variables

3 views
Skip to first unread message

Sandip Chitale

unread,
Jan 8, 2003, 2:44:45 AM1/8/03
to
Normally -

for (var i in window)
{
alert(i);
}

construct is used to enumerate the properties
of an object in JavaScript. However when I try
to enumerate the user defined variables in the
implicit 'window' object inside a browser I am not
able to do so.

I can access those variables if I know their names
e.g.

window.myVariable

The variable was declared using the following syntax-
<html>
<head>
<script language='javascript'>
var myVariable = 'someValue';
alert(window.myVariable); // this works
for (var i in window)
{
alert(i); // myVariable is not enumerated
}
</script>
</head>
<body>
</body>
</html>

Any clues ?

Steve van Dongen

unread,
Jan 8, 2003, 8:18:14 AM1/8/03
to
On 7 Jan 2003 23:44:45 -0800, sandip...@yahoo.com (Sandip Chitale)
wrote:

Only expandos (i.e. dynamically added properties) will be included
when you enumerate an object's properties. Although you can use
window.variableName to access a global variable, it's not *really* an
expando. AFAIK, it is only intended to be a means of acessing the
global variable when you're in a scope where there's another variable
of the same name.

If you make it into a proper expando, e.g.
window.myVariable = "someValue"
then it shows up in your enumeration.

Regards,
Steve

Sandip Chitale

unread,
Jan 8, 2003, 2:31:34 PM1/8/03
to
Thanks Steve for the answer. That explains it.
It begs further the question though where to the
var declared variables go (scopewise) and
is there a way to enumerate them ?

In other words -

The variables declared with

var varName = varValue;

will not be enumerated by the -

for (i in window) {
}

However if I change it to -

window.varName = varValue;

then varName will be enumerated by the for
construct. I can also access it with the same
syntax as I would in the first case.

Steve van Dongen <ste...@hotmail.com> wrote in message news:<sf7o1vsn447c386ci...@4ax.com>...

Steve van Dongen

unread,
Jan 11, 2003, 2:23:56 AM1/11/03
to
On 8 Jan 2003 11:31:34 -0800, sandip...@yahoo.com (Sandip Chitale)
wrote:

>Thanks Steve for the answer. That explains it.


>It begs further the question though where to the
>var declared variables go (scopewise) and
>is there a way to enumerate them ?
>
>In other words -
>
>The variables declared with
>
>var varName = varValue;
>
>will not be enumerated by the -
>
>for (i in window) {
>}
>
>However if I change it to -
>
>window.varName = varValue;
>
>then varName will be enumerated by the for
>construct. I can also access it with the same
>syntax as I would in the first case.

No, there's no way to enumerate global variables that I know of.
Frankly I can't really see any scenario for it. What are you trying
to do that you need to do this?

Regards,
Steve

Sandip Chitale

unread,
Jan 11, 2003, 1:02:45 PM1/11/03
to
Steve van Dongen <ste...@hotmail.com> wrote in message news:<0ghv1v8kttku10fa3...@4ax.com>...

Steve,

I am trying to write an frameset/frame/object browser. I have figured
out one way by parsing the var declarations in the scripts collection
of document object. I am using the regexp for that. It only works for
simple var declarations i.e. var foo;. I want to make it complete by
parsing for -

var foo;
var foo = value;
var foo, bar;
var foo = value, bar = value, baz;

Any suggestions ?

Regards,
sandip

Sandip Chitale

unread,
Jan 18, 2003, 2:23:19 PM1/18/03
to
Well here is the code:

function dumpVars()
{
var varDeclsMatcher =
new RegExp("var\\s((?:\\s*(?:\\w+)\\s*(?:;|,|=[^,;]+[,;])\\s*)+)", "g");
var oneVarMatcher =
new RegExp("\\s*(\\w+)\\s*(?:;|,|=[^,;]+[,;])\\s*", "g");

var scripts = document.getElementsByTagName('script');
for (var i = 0; i < scripts.length; i++) {
if (scripts[i]) {
var scriptText = document.scripts[i].text;
if (scriptText) {
var matched;
while ((matched = varDeclsMatcher.exec(scriptText)) != null) {
var varsOnly = RegExp.$1;
var subMatched;
while ((subMatched = oneVarMatcher.exec(varsOnly)) != null) {
alert(RegExp.$1);
}
}
}
}
}
}

sandip...@yahoo.com (Sandip Chitale) wrote in message news:<b607d812.03011...@posting.google.com>...

0 new messages