Skip to content

Instantly share code, notes, and snippets.

@miya2000
Created July 3, 2010 03:19
Show Gist options
  • Select an option

  • Save miya2000/462266 to your computer and use it in GitHub Desktop.

Select an option

Save miya2000/462266 to your computer and use it in GitHub Desktop.
//http://d.hatena.ne.jp/amachang/20100624/1277359266
var isElementInDocument = (function() {
function isElementInDocumentA(node) {
return node != null && (node.nodeType == 9 || (node.compareDocumentPosition(node.ownerDocument) & 1) == 0);
}
function isElementInDocumentB(node) {
return node != null && (node.nodeType == 9 || node.ownerDocument.documentElement === node || node.ownerDocument.documentElement.contains(node));
}
function isElementInDocumentC(node) {
var target = node;
while(target) {
if (target.nodeType == 9) return true;
target = target.parentNode;
}
return false;
}
return document.compareDocumentPosition ? isElementInDocumentA :
document.documentElement.contains ? isElementInDocumentB :
isElementInDocumentC ;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment