All:
When I try to follow a JS DOM memory leak tutorial, there is one example of memory leak about OLD IE(7, 8):
<div id="myDiv">
<button id="myBtn">Click Me</button>
</div>
<script type="text/javascript">
var btn = document.getElementById("myBtn");
btn.onclick = function(){
document.getElementById("myDiv").innerHTML = "Processing...";
}
</script>
And the solution it gives is:
<div id="myDiv">
<button id="myBtn">Click Me</button>
</div>
<script type="text/javascript">
var btn = document.getElementById("myBtn");
btn.onclick = function(){
btn.onclick = null;
document.getElementById("myDiv").innerHTML = "Processing...";
}
</script>
My confuse here:
- What is the leak: the myBtn DOM object or the onclick function?(my understanding: it tries to say the DOM, because that innerHTML tries to replace the whole button node with a text node)
- If the DOM is the leak, then that
btn
variable still references to the DOM, which causes it can not be GCed, why this can be the solution?
Thanks