get prototype name of object in IE?

Discussion in 'Javascript' started by Brian Genisio, Feb 4, 2004.

  1. Hi all,

    Does anyone know of a way in IE to determine the prototype name of an
    object?

    For instance, in Mozilla, I can say:

    junk = document.getElementById("myID");
    alert(junk.toString());

    and it will tell me "[object <object name>]". In IE, I only get
    "[object]".

    How do I get the prototype name of an object in IE? Is there a way?

    Brian
     
    Brian Genisio, Feb 4, 2004
    #1
    1. Advertising

  2. Brian Genisio

    Grant Wagner Guest

    Brian Genisio wrote:

    > Hi all,
    >
    > Does anyone know of a way in IE to determine the prototype name of an
    > object?
    >
    > For instance, in Mozilla, I can say:
    >
    > junk = document.getElementById("myID");
    > alert(junk.toString());
    >
    > and it will tell me "[object <object name>]". In IE, I only get
    > "[object]".
    >
    > How do I get the prototype name of an object in IE? Is there a way?
    >
    > Brian


    I used the following code to generate a list of all attributes of "junk":

    <span id="myID">x</span>
    <script>
    var junk = document.getElementById("myID");
    var s = [];
    for (var i in junk) {
    s.push(i + ' = ' + junk);
    }
    document.write(s.join('<br>'));
    </script>

    Then looked for something that could tell me what kind of HTML element
    "junk" is. It appears junk.tagName reveals that information as "SPAN". I
    then tested the same code in Mozilla Firebird 0.7, and it also has a
    property called tagName, with the same value.

    So for IE and Gecko-based browsers, you can use:

    var junk = document.getElementById("myID");
    if (junk != null) {
    var htmlTag = junk.tagName;
    }

    Unfortunately, Opera 7.23 does not contain a tagName attribute, and using
    alert(junk.toString()) produces the incredibly unhelpful [object
    HTMLElement].

    --
    | Grant Wagner <>

    * Client-side Javascript and Netscape 4 DOM Reference available at:
    *
    http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/frames.html

    * Internet Explorer DOM Reference available at:
    *
    http://msdn.microsoft.com/workshop/author/dhtml/reference/dhtml_reference_entry.asp

    * Netscape 6/7 DOM Reference available at:
    * http://www.mozilla.org/docs/dom/domref/
    * Tips for upgrading JavaScript for Netscape 7 / Mozilla
    * http://www.mozilla.org/docs/web-developer/upgrade_2.html
     
    Grant Wagner, Feb 4, 2004
    #2
    1. Advertising


  3. > Unfortunately, Opera 7.23 does not contain a tagName attribute, and using
    > alert(junk.toString()) produces the incredibly unhelpful [object
    > HTMLElement].
    >


    Hmmmm... I am not sure you understand what I am looking for. tagName is
    not what I am looking for. The tagName element, part of the DOM spec
    for HTMLElements, tells exactly that... the tag name.

    So, for <HTML>, you will get HTML. Although that is useful to some, I
    am looking for the actual object name... in this case, it would be
    HTMLHtmlElement in Mozilla. It tells me so, if I say
    htmlObject.toString(). More specifically, it says [object
    HTMLHtmlElement].

    That is, precisely, what I am trying to get from IE. I dont want to
    know the tag name... I want to know the object name. Anything that is
    not an element does not have the tagName option, so would fail if you
    called the tagName.

    Note: This is not for scripting purposes... it is for my own
    documentation purposes.

    Thank you,
    Brian
     
    Brian Genisio, Feb 4, 2004
    #3
  4. "Grant Wagner" <> wrote in message
    news:...
    <snip>
    >Unfortunately, Opera 7.23 does not contain a tagName attribute, ...

    <snip>

    I think you will find that Opera does have a tagName property and your
    script is not finding it because Opera is less than forthcoming with -
    for(var prop in obj) - loops with host objects.

    Richard.
     
    Richard Cornford, Feb 4, 2004
    #4
  5. Brian Genisio

    Jim Ley Guest

    On Wed, 04 Feb 2004 15:54:24 -0500, Brian Genisio
    <> wrote:

    >That is, precisely, what I am trying to get from IE. I dont want to
    >know the tag name... I want to know the object name.


    It doesn't do the same, you can create it with

    IHTML str Element

    replacing " str " with the tagname - I can't possibly see the point of
    it though, and there may well be some exclusions to that anyway.

    Jim.
    --
    comp.lang.javascript FAQ - http://jibbering.com/faq/
     
    Jim Ley, Feb 5, 2004
    #5
  6. Jim Ley wrote:

    > On Wed, 04 Feb 2004 15:54:24 -0500, Brian Genisio
    > <> wrote:
    >
    >
    >>That is, precisely, what I am trying to get from IE. I dont want to
    >>know the tag name... I want to know the object name.

    >
    >
    > It doesn't do the same, you can create it with
    >
    > IHTML str Element
    >
    > replacing " str " with the tagname - I can't possibly see the point of
    > it though, and there may well be some exclusions to that anyway.
    >
    > Jim.


    Thanks for the try... but that is still not what I am looking for.

    Every object type in JavaScript is a single type, an object. This is
    why when you say typeof myObject, you get "object", no matter what it is.

    With each object, there exists a prototype. That prototype defines the
    object name (HTMLHtmlElement, DOMNode, DOMElement, HTMLElement, etc), as
    well as the properties and methods (which are really function object
    properties) associated with the object.

    When you do a myObject.toString in Mozilla, it prints the prototype name
    of the object, such as HTMLHtmlElement. (toString is overridden for a
    few objects, such as the Location object).

    Anyways, I am looking to see if there is any way to get that value. The
    ECMA binding for the DOM specification calls out the types that are to
    exist, such as DOMNode, DOMElement, HTMLElement, HTMLHtmlElement,
    DOMDocument and HTMLDocument. It does not call out the more specific
    object names, such as the Location, History, Navigator and Window objects.

    I am trying to get these values, so I can know what IE calls them.

    I know it is cooky, and I know it looks like there is no good reason to
    get that information, but I promise it is useful to me. Although, I can
    live without it as well :)

    Thanks,
    Brian
     
    Brian Genisio, Feb 5, 2004
    #6
  7. "Brian Genisio" <> wrote in message
    news:40229ab9$1@10.10.0.241...
    <snip>
    >With each object, there exists a prototype. That prototype
    >defines the object name (HTMLHtmlElement, DOMNode, DOMElement,
    >HTMLElement, etc), as well as the properties and methods (which
    >are really function object properties) associated with the object.


    ECMAScript distinguishes Host Objects from Native Objects (section
    4.3.6-8) and makes no requirements of Host Objects. All of the DOM nodes
    are Host Objects so whether they have prototypes or not is left up to
    the implementers to decide. Some have decided that they will, others
    have decided that they don't (or not to expose those properties to
    scripts).

    <snip>
    >Anyways, I am looking to see if there is any way to get that
    >value. The ECMA binding for the DOM specification calls out
    >the types that are to exist, such as DOMNode, DOMElement,
    >HTMLElement, HTMLHtmlElement, DOMDocument and HTMLDocument.
    >It does not call out the more specific object names, such as
    >the Location, History, Navigator and Window objects.

    <snip>

    The W3C specifications define HTMLElement, HTMLHtmlelement, etc. as
    interfaces and not classes. It is completely practical for a Javascript
    object to be no more than an object (with Object.prototype as its
    prototype) and still implement any interface.

    Richard.
     
    Richard Cornford, Feb 5, 2004
    #7
  8. Brian Genisio <> wrote in message news:<40229ab9$1@10.10.0.241>...


    hi Brian,

    > With each object, there exists a prototype. That prototype defines the
    > object name (HTMLHtmlElement, DOMNode, DOMElement, HTMLElement, etc), as
    > well as the properties and methods (which are really function object
    > properties) associated with the object.


    what you call prototype is the constructor of an object.

    > When you do a myObject.toString in Mozilla, it prints the prototype name
    > of the object, such as HTMLHtmlElement. ( ... ).


    for objects in mozilla thats types get identified by the "typeof"
    operator as "object" the "toString" method in mozilla returns this
    type and additionally, separated by a blank, the objects constructor
    name (arrays will be traeted differently);

    your problem can't be solved since you are looking for the constructor
    property of html node elements in msie, but this browsers node objects
    have not implemented the constructor property. for this you won't be able
    to assamble a "toString" method for msie that would bahave mozilla like.

    proof it:

    alert(document.getElementsByTagName("body")[0]);
    returns "[object]" in msie6 and "[object HTMLBodyElement]" in mozilla.

    alert(document.getElementsByTagName("body")[0].constructor);
    returns "undefined" in msie6 and "[HTMLBodyElement]" in mozilla.


    so long - peterS. -
     
    peter seliger, Feb 6, 2004
    #8
    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. ding feng
    Replies:
    2
    Views:
    2,852
    ding feng
    Jun 25, 2003
  2. June Lee
    Replies:
    2
    Views:
    820
    Jim Cobban
    Apr 13, 2008
  3. Replies:
    9
    Views:
    214
    Thomas 'PointedEars' Lahn
    May 26, 2006
  4. Replies:
    3
    Views:
    278
  5. javascript fish
    Replies:
    0
    Views:
    181
    javascript fish
    Oct 11, 2008
Loading...

Share This Page