QAZ
QAZ

Reputation: 4930

Enumerate JavaScript objects functions?

I wish to enumerate all the available functions of various JavaScript objects and even HTML elements created in JavaScript. For example the following works great in both Chrome and FireFox:

<html>
    <body>
    <script>

        var object = document.createElement( "select" );

        for( var prop in object )
        {
            document.body.innerHTML += "" + prop + "; // " + typeof object[prop] + "<br/>";
        }

    </script>
    </body>
</html>

This outputs all the properties of the object including the functions available to that object, e.g.:

...
insertAdjacentHTML; // function
insertAdjacentText; // function
insertAdjacentElement; // function
getAttribute; // function
setAttribute; // function
removeAttribute; // function
getAttributeNode; // function
...

However this will not work in IE9, all you get are the string/number/object properties and never any of the function properties.

My question is how can I discover at run time what function names are exported by an object in IE9?

Many thanks in advance.

UPDATE: adding a doctype gets this working as expected.

<!DOCTYPE html>
    <body>
    <script>

        var object = document.createElement( "select" );

        for( var prop in object )
        {
            document.body.innerHTML += "" + prop + "; // " + typeof object[prop] + "<br/>";
        }

    </script>
    </body>
</html>

Upvotes: 4

Views: 1179

Answers (2)

wsanville
wsanville

Reputation: 37516

This will work fine in IE if you specify a DOCTYPE. Without a DOCTYPE, IE will render in Quirks Mode, which is essentially IE 5.5's behavior, which will greatly effect IE's Javascript support.

<!doctype html>
<html>
    <body>
    <script>

        var object = document.createElement( "select" );

        for( var prop in object )
        {
            document.body.innerHTML += "" + prop + "; // " + typeof object[prop] + "<br/>";
        }

    </script>
    </body>
</html>

Result:

form; // object
length; // number
multiple; // boolean
name; // string
options; // object
selectedIndex; // number
size; // number
type; // string
value; // string
dataFld; // string
dataFormatAs; // string
dataSrc; // string
add; // function
item; // function
namedItem; // function
remove; // function
.
.
.

Upvotes: 2

Jerod Venema
Jerod Venema

Reputation: 44632

That code looks fine to me, and works OK in IE9 here.

http://jsbin.com/ivukus/edit#preview

Upvotes: 4

Related Questions