Wednesday, October 2, 2013

How to fully remove temporary elements?

How to fully remove temporary elements?

I am dynamically adding a 'div' node inside a function that will only be
needed temporarily. I have already used < parent >.removeChild to detach
it and then set it to null, but I am curious to know what happens to
unattached nodes created in a function when it goes out of scope?
If the nodes remain in memory but unattached:
A) Is setting to null even necessary or does it automatically go away on
return?
and if so is setting to null the proper way?
B) Does anything need to be done with its properties? (such as
node.style.position)
C) If it contained additional children, would they need to be handled
individually or would they go away when their unattached parent node is
set to null?
D) What tool(s) can be used to view these kinds of unattached resources?
Here is an example function for discussion purposes:
function dpi(){
var res={},
div=document.createElement("div"),
body=document.getElementsByTagName('body')[0]
div.style.position="absolute"
div.style.width="1in"
div.style.height="1in"
div.style.left="-100%"
div.style.top="-100%"
body.appendChild(div)
res["xdpi"]=div.offsetWidth
res["ydpi"]=div.offsetHeight
body.removeChild(div)
div=null
return res
}

No comments:

Post a Comment