I'm trying to get:
document.createElement('div') //=> true
{tagName: 'foobar something'} //=> false
In my own scripts, I used to just use this since I never needed tagName
as a property:
if (!object.tagName) throw ...;
So for the 2nd object, I came up with the following as a quick solution -- which mostly works. ;)
Problem is, it depends on browsers enforcing read-only properties, which not all do.
function isDOM(obj) {
var tag = obj.tagName;
try {
obj.tagName = ''; // read-only for DOM, should throw exception
obj.tagName = tag; // restore for normal objects
return false;
} catch (e) {
return true;
}
}
Does anyone know a good substitute?