473,388 Members | 1,824 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,388 software developers and data experts.

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 2255
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: David Bear | last post by:
Let's say I have a list called, alist. If I pass alist to a function, how can I get the name of it? alist = range(10) def afunction(list): listName = list.__name__ (fails for a list object)
4
by: Daniel | last post by:
Hullo =) Inspired by another guy's questions here I've created an isset function that works (almost) like the one in native PHP: function isset(variablename) {...
2
by: Raptor | last post by:
I'm a complete Javascript n00b, using a snippet I found on the web. I'll probably be buying a Javascript book. What's the authoritative on-line resource for Javascript, like php.net is for PHP? ...
3
by: Derek Basch | last post by:
Is it bad form to use the global window variable to reference an event handlers window? Like so: function SortableTable() { oFilterAdd = this.document.createElement("button");...
5
by: zkvneml | last post by:
Hi guys, Is there a way to retreave the javascript functions' names on the page? The javascript functions are properties of the global object (window/frame), but I do not know how to collect...
5
by: Little | last post by:
I have this program and I need to work on the test portion, which tests if a Val is in the list. It returns false no matter what could you look at the part and see what might need to be done to fix...
26
by: yb | last post by:
Hi, Is there a standard for the global 'window' object in browsers? For example, it supports methods such as setInterval and clearInterval, and several others. I know that w3c standardized...
5
by: Jay | last post by:
Hey There, I have this function that is called through EnumWindows: BOOL CALLBACK EnumOpenWindows(HWND hwnd, LPARAM lParam) { DWORD dwID; LPDWORD dwID2; LPDWORD currProcId;...
4
by: alexandre.brisebois | last post by:
Hi, I am using access 2003, I would like to know if there is an option to reorganize the tables in a maner that is readable, as we can do in sql sever 2000 or 2005. I have been given a database...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.