What happens to Javascript when page navigates away? A book I am reading is instructing to clear things on "unload" event.
But what's the point? doesn't everything get lost and released when pages moves away in browser?
Thanks,
Sean
What happens to Javascript when page navigates away? A book I am reading is instructing to clear things on "unload" event.
But what's the point? doesn't everything get lost and released when pages moves away in browser?
Thanks,
Sean
The only case where this is useful is in clearing event handlers that have cyclical references between the DOM and JS engines (not always event handlers, but it's the most common way) and therefore caused memory leaks. Everything else is garbage collected when you unload the page
See this post Javascript memory leaks after unloading a web page
Usually the browser does a good job of cleaning up memory after page navigation. Howerver, there are things you can do to confuse the browser and cause it to hang on to memory between page transitions. This is particularly true when you are storing data in DOM elements, or have circular references between the DOM and your script.
This was a bigger issue a few years ago in older browsers. Current browser releases are better about memory cleanup between page transitions.
It may make sense to do some clean-up during page unload, but it's even better to modify your design to eliminate circular references. Don't store data in the DOM.
Here's a good article for more detail.
An article at MDN regarding JavaScript leaks in FireFox.
Here's a Microsoft Knowledge Base article with IE specific detail.
Sure. But you could free some memory in the browser by unloading (not really useful nowadays, but useful in the past). Or you can do some action on unload (i.e. asking user confirmation, saving stuff, etc...).