Last active
August 30, 2017 08:38
-
-
Save vikynandha-zz/6539809 to your computer and use it in GitHub Desktop.
Check if given object is a DOM element
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function isElement(obj) { | |
try { | |
//Using W3 DOM2 (works for FF, Opera and Chrom) | |
return obj instanceof HTMLElement; | |
} | |
catch(e){ | |
//Browsers not supporting W3 DOM2 don't have HTMLElement and | |
//an exception is thrown and we end up here. Testing some | |
//properties that all elements have. (works on IE7) | |
return (typeof obj==="object") && | |
(obj.nodeType===1) && (typeof obj.style === "object") && | |
(typeof obj.ownerDocument ==="object"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment