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 ?
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
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>...
>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
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
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>...