dltypeof - Custom JS typeof operator - DHTML Lab | 3 | WebReference

dltypeof - Custom JS typeof operator - DHTML Lab | 3


JavaScript Programming: dltypeof() v1.0,
An Introduction

DOM Objects

dltypeof() will identify the two most often used types of DOM objects: elements and collections.

DOM Elements

DOM elements ( HTML tags ) have a type of "domelement".

DOM text nodes have a type of "textnode".

IE for Windows also supports the TextRange object. It can be neither created nor referred to in any other browser, but it has a type of "textrange" in IE-only applications.

DOM Collections

dltypeof() will return "domcollection" for all collections whether they are represented by properties like anchors or by methods that return collections like getElementsByTagName().

dltypeof() also handles browser-specific collections. Some, like all, can be referenced in cross-browser applications, often for browser-identification. These collections have a type of "undefined" if they are not supported.
Other browser-specific collections, like text rectangle, cannot be referenced in cross-browser applications, as they are created by unsupported methods.

In general, dltypeof() will handle return values correctly for the environment it resides in, whether it is a cross-browser script or a browser-specific script.

The window and window.frames Problem

The window object and the window.frames collection are identified as the same object in both Netscape and IE. That is, not as objects of similar types but the same object, with the same property values and methods and they are interchangeable in use. This, of course, makes no sense (at least not to me), and it could be a holdover from the original JavaScript implementation.

It is therefore not possible to differentiate between the two and identify window as a special object type ( "window" ) and window.frames as a collection of type "domcollection".

This problem does not exist in Opera, where window and window.frames are separate objects of different types.

However, dltypeof() cannot return different values for the same object in different browsers if the object is supported in all browsers. So, both window and window.frames have a type of "window" in all browsers.

The following table lists all the possible return values for DOM objects as well as the original typeof() return values

DOM objecttypeof() return value dltypeof() return value
internet explorer calendar netscapecalendar
HTML element
( DIV, IMG, P, TABLE, etc. )
text nodeobjecttextnode
text rangeobjecttextrangen/a
Standard collections
( anchors, applets, areas, attributes, cells, childNodes, elements, embeds, forms, images, links, options, rows, tbodies, etc. )
IE collections - supported by Opera
( all, children, scripts, tags, etc. )
IE collections - IE only
( behavioururns, filters, document.frames, namespaces, etc. )
IE collections - IE only - IE-specific applications
( control range, imports, pages, rules, text rectangle, etc. )
Non-IE collections
( mimeTypes, plugins )
IE and NS collections
( styleSheets )

The dltypeof() function is listed on the final page.

Send a comment or suggestion to Peter Belesis

Created: August 23, 2004
Revised: August 23, 2004

URL: http://webreference.com/dhtml/column68/3.html

Justtechjobs.comFind a programming school near you

Online Campus Both