I’m making a lightbox, do I need to use the
$(window.top).unload(function() { .. });
to do clean up of variables, or will all browsers automatically clean up everything and reset memory and what not?
Do I need to bother or will the browser do it for me? What with all the memory leaks and all..
Edit: I attached a lot of data to div elements. So do I need to do removeData at unload?
There are a couple different ways to create memory leaks in javascript. Most have to do with creating circular references between javascript objects and the DOM. see:
http://www.ibm.com/developerworks/web/library/wa-memleak/
but otherwise, no, you do not have to explicitly delete references to things – the javascript garbage collector will do this for you.
The code snippet that you posted has a side effect that you should be aware of, however. If you attach anything to the
onunloadhandler for the document, then browsers that support caching the state of the DOM/javascript engine for fast retrieval when navigating backwards/forwards are no longer able to keep the page in thebfcache(see http://www.webkit.org/blog/516/webkit-page-cache-ii-the-unload-event/). This can have performance implications.