Skip to content

Instantly share code, notes, and snippets.

@kangax
Created August 13, 2008 03:51
Show Gist options
  • Save kangax/5191 to your computer and use it in GitHub Desktop.
Save kangax/5191 to your computer and use it in GitHub Desktop.
Element.Methods.descendantOf = (function() {
var doc = document.documentElement;
// DOM Level 3 approach (Gecko, Opera)
if ('compareDocumentPosition' in doc) {
return function(element, ancestor) {
return (element.compareDocumentPosition(ancestor) & 8) === 8
};
}
// IE, Safari
if ('contains' in doc) {
return function(element, ancestor) {
return ancestor !== element && ancestor.contains(element)
};
}
// fallback on while loop
return function(element, ancestor) {
while (element = element.parentNode)
if (element === ancestor) return true;
return false;
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment