isElement - determining if an object is an element



Due to M$'s stupidity in not making DOMElements first class citizens the
following will not work :-

function isElement( o)
{
return o instanceof Element
}

It works for FF, Opera and Safari.

What prototype does is this :-

isElement: function(object) {
return object && object.nodeType == 1
}

My version used Browser sniffing :-

function isElement( o)
{
if (!isIE)
return o instanceof Element
else
return o && o.nodeType == 1 && o.tagName != undefined
}

Test case :-

http://www.aarongray.org/Test/JavaScript/isElement-test.html

The actual 'isIE' test code is not the best but is used for brevity.

Any crevats, problems or enhancements most welcome.

Thanks,

Aaron


.



Relevant Pages

  • Re: isElement - determining if an object is an element
    ... return o instanceof Element ... Its not a JSON string but an diseminated object at this stage that comes from a JSON object. ...
    (comp.lang.javascript)
  • Re: isElement - determining if an object is an element
    ... return o instanceof Element ... This fails because it cannot be used universally, and will result in different behaviours accross browsers if used as the general case with a sub case for |IE. ... Any comments, cristisms, or general flac welcome:) ...
    (comp.lang.javascript)