Reputation: 571
I was busy making a JavaScript code that allows me to make GUI page elements to have custom context menus, when I thought about checking to see if an element exists in order to make a context menu for it.
function element_exists(el){
var tagName=el.tagName;
var exists=0;
for(var a=0;a<document.getElementsByTagName(tagName).length;a++){
if(document.getElementsByTagName(tagName)[a]==el){
exists=1;
}
}
return exists;
}
In this code, I pass a reference to a DOM element object (which is stored from earlier). Let's say that it was stored, but since then I removed the element itself from the document.
I'm using Chrome Canary, and even if I edit the page through the console and make a new element with the exact same tag name and ID, it returns false. Would it return true if it had the same innerText and innerHTML?
If not, is this a standard in all web browsers (old and new)? Just curious because I could eliminate some unnecessary code if they're all unique.
Upvotes: 1
Views: 103
Reputation: 15159
I'm pretty sure the answer is no; each element is unique, regardless of whether they have similar values (including "id").
This might provide you some insight into how element garbage collection works in Chrome. I'm not sure how the other browsers respond though.
http://www.html5rocks.com/en/tutorials/memory/effectivemanagement/
It outlines some tools that might be useful to test your theory.
Upvotes: 1