enumerating 'window' for getting the list of global var names in IE

S

sundew

so here is the test case:

<head>
<script type='text/javascript'>
// this function simply returns the number of enumerable
namespaces(objects)
function numNSpaces(){
var num = 0;
for(var i in window)
++num;
return num;
};

// get the number of enumerable elements of the 'window'
var numBefore = numNSpaces();
// load a javascript file dynamically at this point.
// and 'json.js' file trivially contains a single js line: "var json =
{id:0};"
load('json.js');
</script>

<script type='text/javascript'>
// get the number of enumerable elements of thie 'window' after
'json.js' loading
var numAfter = numNSpaces();
// assertion-1
assert(numBefore != numAfter, 'all IE versions will fail on this');
// assertion-2
assert(typeof json == 'object', 'but the newly introduced var is
accessable');
// assertion-3
assert(typeof window.json == 'object', 'access var through
"window.json"');
// assertion-4
assert(typeof window['json'] == 'object', 'access var using
window["json"]');
</script>
</head>

I tested it on Opera7, FF1.5, Netscape6, Konqueror3.5.1 and IE5.0 &
IE6.0.
All browsers passed these assertions, but all IE versions were failed
at the 'assertion-1'.
It seems user defined global vars are not enumerable by 'for(var i in
window)' in IEs.
Is there anybody know how to get the names of all top-level namespaces
in IE?
Any input will be greatly appreciated. Thank you.
 
T

Thomas 'PointedEars' Lahn

sundew said:
so here is the test case:

<head>
<script type='text/javascript'>
// this function simply returns the number of enumerable
namespaces(objects)

Code should be posted (and written, but maybe that's just me) so that it
is executable as is, i.e. so that it is not subject to common word-wrapping.
function numNSpaces(){
var num = 0;
for(var i in window)
++num;
return num;
};

// get the number of enumerable elements of the 'window'
var numBefore = numNSpaces();
// load a javascript file dynamically at this point.
// and 'json.js' file trivially contains a single js line: "var json =
{id:0};"
load('json.js');
</script>

<script type='text/javascript'>
// get the number of enumerable elements of thie 'window' after
'json.js' loading
var numAfter = numNSpaces();
// assertion-1
assert(numBefore != numAfter, 'all IE versions will fail on this');
[...]
</script>
</head>

Your HTML markup is not Valid without a `title' element.
[...]
All browsers passed these assertions, but all IE versions were failed
at the 'assertion-1'.
It seems user defined global vars are not enumerable by 'for(var i in
window)' in IEs.

`window' is not the Global Object, of which global variables are a property.
It is a host-defined property of the Global Object that is said to refer to
its owner. Provided that your load() method does what it is intended to do
in IE too (you have not showed its code), I take your test as a proof that
the latter is not generally true. (No surprise here.)
Is there anybody know how to get the names of all top-level namespaces
in IE?

Try

var _global = this;

function numNSpaces()
{
var num = 0;
for (var i in _global) ++num;
return num;
}

See also <URL:http://pointedears.de/ObjectInspector>.

BTW: The term "(enumerable) namespaces" is misleading here, at best.


PointedEars
 
S

sundew

First of all, thank you for your help.
Code should be posted (and written, but maybe that's just me) so that it
is executable as is, i.e. so that it is not subject to common word-wrapping.

It was not just you. I should have 'preview'ed before submission. I
apologize for that.
I have tried what you suggested but hit on the same wall.
Here's the whole code. Hope it look fine this time. It looks fine in
preview. =)
Thank you again.

//---------------------------> [code begin]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Test - getting list of global vars</title>
<script type='text/javascript'>
// load js
function load(js){
document.write('<script type="text/javascript" src="'+ js
+'"><\/script>');
};

// simple assert implementation
function assert(pred, title){
if(!pred){
alert('[assertion failure]\n'+ title);
}
};

// returns num of window properties
function numNSpaces(){
var num = 0;
for(var i in window)
++num;
return num;
};

// get the number of enumerable elements of the 'window'
var numBefore = numNSpaces();

// load a javascript file dynamically at this point.
// * file, 'json.js' contains a single line of code:
// var json = {id:0};
load('json.js');
</script>

<script type='text/javascript'>
// get the number of enumerable elements again
var numAfter = numNSpaces();
// assertion-1
assert(numBefore != numAfter, 'all IE versions will fail on this');
// assertion-2
assert(typeof json == 'object', 'accessing method - 1');
// assertion-3
assert(typeof window.json == 'object', 'accessing method - 2');
// assertion-4
assert(typeof window['json'] == 'object', 'accessing method - 3');
</script>
</head><body></body></html>
//<--------------------------- [code end]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top