469,354 Members | 2,046 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,354 developers. It's quick & easy.

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

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.

--
Sundew H. Shin
Developer/Manager
Wednus Project
http://wednus.com

Apr 6 '06 #1
3 2078
sundew wrote:
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
Apr 6 '06 #2
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]

--
Sundew H. Shin
Developer/Manager
Wednus Project
http://wednus.com

Apr 6 '06 #3
sundew wrote:
<snip>
It seems user defined global vars are not enumerable by
'for(var i in window)' in IEs.

<snip>

That is the situation. Eric Lippert admits that they F***ed up when the
implemented JScript:-

<URL: http://blogs.msdn.com/ericlippert/ar...04/414684.aspx >

- but doesn't hold out much hope of it being fixed soon, if ever.

Richard.
Apr 7 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by David Bear | last post: by
2 posts views Thread by Raptor | last post: by
3 posts views Thread by Derek Basch | last post: by
5 posts views Thread by zkvneml | last post: by
5 posts views Thread by Little | last post: by
26 posts views Thread by yb | last post: by
5 posts views Thread by Jay | last post: by
4 posts views Thread by alexandre.brisebois | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.