dltypeof - Custom JS typeof operator - DHTML Lab | 3
dltypeof() will identify the two most often used types of DOM objects: elements and collections.
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.
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
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 object||typeof() return value||dltypeof() return value|
( DIV, IMG, P, TABLE, etc. )
( 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. )
( mimeTypes, plugins )
IE and NS collections|
( styleSheets )
The dltypeof() function is listed on the final page.
Revised: August 23, 2004