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

Discussion in 'Javascript' started by sundew, Apr 6, 2006.

  1. sundew

    sundew Guest

    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
     
    sundew, Apr 6, 2006
    #1
    1. Advertising

  2. 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
     
    Thomas 'PointedEars' Lahn, Apr 6, 2006
    #2
    1. Advertising

  3. sundew

    sundew Guest

    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
     
    sundew, Apr 6, 2006
    #3
  4. 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/archive/2005/05/04/414684.aspx >

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

    Richard.
     
    Richard Cornford, Apr 7, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alvin Bruney

    Threads.. Session var lost, App var ok

    Alvin Bruney, Dec 2, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    385
    rooster575
    Dec 2, 2003
  2. Kevin
    Replies:
    1
    Views:
    397
    Richard Bos
    Feb 24, 2004
  3. wanwan
    Replies:
    3
    Views:
    455
    Alex Martelli
    Oct 14, 2005
  4. runsun pan
    Replies:
    2
    Views:
    211
    runsun pan
    Oct 12, 2006
  5. intrader
    Replies:
    5
    Views:
    119
    intrader
    Jan 11, 2010
Loading...

Share This Page