It's logical, have you attempted reviewing the source code of your page after
the function has completed? The body tag may no longer be present - taking your event listener with it (or it may still be in memory, hanging uselessly).
Try connecting it to the window
.
window.onresize = function()
{
document.write((this.innerWidth/this.innerHeight)*100+'%');
};
In older versions of a once very popular browser, attaching event listeners to the window object could lead to memory leaks. The window object is never completely destroyed more information available here if needed.
How can the memory issue be resolved?
(function(win)
{
win.onresize = function()
{
document.write((this.innerWidth/this.innerHeight)*100+'%');
};
if (win.attachEvent)
{
win.attachEvent('onbeforeunload',(function(self)
{
return function freeMem()
{
self.onresize = null;
self.detachEvent('onbeforeunload',freeMem);
};
})(win));
return;
}
win.addEventListener('beforeunload',(functio(self)
{
return function freeMem()
{
self.onresize = null;
self.removeEventListener('beforeUnload',freeMem,false);
};
})(win),false);
})(this);//this points to current window
I understand, it may look messy, but I have tested this method and it works - Thanks to IE for that. Technically, the closures passing win
to self
could be omitted, but I have included it to be absolutely certain. Once the onbeforeunload
event triggers and the handler returns, all functions and references go out of scope, giving the GC's no reason to not free the memory.
It may be excessive in most cases, but just in case someone finds it useful, I decided to share it here... :-P