Last active
August 30, 2016 20:02
-
-
Save ozthekoder/daf485738ecff4c86edc7f69d5018a3f to your computer and use it in GitHub Desktop.
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
document.onclick= function(event) { | |
if (event===undefined) event= window.event; // IE hack | |
var target= 'target' in event? event.target : event.srcElement; // another IE hack | |
var root= document.compatMode==='CSS1Compat'? document.documentElement : document.body; | |
var path= '//' + getPathTo(target); | |
console.log(path); | |
} | |
function getPathTo(element) { | |
if (element===document.body) | |
return element.tagName; | |
var ix= 0; | |
var siblings= element.parentNode.childNodes; | |
for (var i= 0; i<siblings.length; i++) { | |
var sibling= siblings[i]; | |
if (sibling===element) | |
return getPathTo(element.parentNode)+'/'+element.tagName+'['+(ix+1)+']'; | |
if (sibling.nodeType===1 && sibling.tagName===element.tagName) | |
ix++; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment