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 object | typeof() return value | dltypeof() return value | ||
---|---|---|---|---|
HTML element ( DIV, IMG, P, TABLE, etc. ) | object | domelement | ||
text node | object | textnode | ||
text range | object | textrange | n/a | |
Standard collections ( anchors, applets, areas, attributes, cells, childNodes, elements, embeds, forms, images, links, options, rows, tbodies, etc. ) | object | domcollection | ||
IE collections - supported by Opera ( all, children, scripts, tags, etc. ) | object | domcollection | undefined | |
IE collections - IE only ( behavioururns, filters, document.frames, namespaces, etc. ) | object | domcollection | undefined | |
IE collections - IE only - IE-specific applications ( control range, imports, pages, rules, text rectangle, etc. ) | object | domcollection | n/a | |
Non-IE collections ( mimeTypes, plugins ) | object | object | mimetypecollection plugincollection | |
IE and NS collections ( styleSheets ) | object | domcollection | undefined | domcollection |
frames | object | window | ||
window | object | window |
The dltypeof() function is listed on the final page.
Send a comment or suggestion to Peter Belesis
Created: August 23, 2004Revised: August 23, 2004
URL: https://webreference.com/dhtml/column68/3.html