I'm writing a userscript and I want to get the "address" of a node, like, if the web page is like this:
<body>
<content>
<a href='/something' name='element I'm interested in'>
</content>
</body>
How do I get the address of a
(where a
is a particular a
element that I know and can point to it manually, or with other regular selectors) so it would give me this: body.content.a
which I can then use in my userscript to perform operations on it like you would on a node gotten by getElementBy() method in javascript.
Obviously the real page would be much more complex and I can't write it all out. Is there a way to get that address either by running jquery or javascript on the page or some browser tool?
Using javascript if you already have the element in some variable and you want to find it's heirachy then it should be possible just by traversing the nodes parent chain: eg something like
function getAddress(element) {
var address = element.tagName;
if (element.parentNode) {
address = getAddress(element.parentNode) + '.' + address;
}
return address;
}
// using the above function by
var node = document.getElementById("myFavouriteNode");
var address = getAddress(node);
Note that that "address" in the format you suggested wont actually be usable for finding nodes or anything as per the comments on your question. You will need to specify how you intend to use the address first.