Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > get prototype name of object in IE?

Reply
Thread Tools

get prototype name of object in IE?

 
 
Brian Genisio
Guest
Posts: n/a
 
      02-04-2004
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

 
Reply With Quote
 
 
 
 
Grant Wagner
Guest
Posts: n/a
 
      02-04-2004
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[i]);
}
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 <(E-Mail Removed)>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_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-deve...upgrade_2.html


 
Reply With Quote
 
 
 
 
Brian Genisio
Guest
Posts: n/a
 
      02-04-2004

> 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


 
Reply With Quote
 
Richard Cornford
Guest
Posts: n/a
 
      02-04-2004
"Grant Wagner" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
<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.


 
Reply With Quote
 
Jim Ley
Guest
Posts: n/a
 
      02-05-2004
On Wed, 04 Feb 2004 15:54:24 -0500, Brian Genisio
<(E-Mail Removed)> 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/

 
Reply With Quote
 
Brian Genisio
Guest
Posts: n/a
 
      02-05-2004
Jim Ley wrote:

> On Wed, 04 Feb 2004 15:54:24 -0500, Brian Genisio
> <(E-Mail Removed)> 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

 
Reply With Quote
 
Richard Cornford
Guest
Posts: n/a
 
      02-05-2004
"Brian Genisio" <(E-Mail Removed)> 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- 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.


 
Reply With Quote
 
peter seliger
Guest
Posts: n/a
 
      02-06-2004
Brian Genisio <(E-Mail Removed)> 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. - (E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Prototype WTP 0.2 released,this release for Prototype 1.6.0 javascript fish Javascript 0 10-11-2008 07:35 AM
Class prototype vs C function prototype June Lee C++ 2 04-13-2008 08:17 PM
Prototype Object.extend(new Base() | Hash | Hash.prototype) usage: jacobstr@gmail.com Javascript 3 03-27-2007 07:56 AM
relation between prototype and Prototype.js shypen42@yahoo.fr Javascript 9 05-26-2006 01:13 AM
Re: Urgent! how to get object name, method name and attribute name based on the strings? ding feng C++ 2 06-25-2003 01:18 PM



Advertisments