get prototype name of object in IE?

B

Brian Genisio

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
 
G

Grant Wagner

Brian said:
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 <[email protected]>

* 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
 
B

Brian Genisio

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
 
R

Richard Cornford

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.
 
J

Jim Ley

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.
 
B

Brian Genisio

Jim said:
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
 
R

Richard Cornford

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).

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.
 
P

peter seliger

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. - (e-mail address removed)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top