My company does this by running an ajax query to save every 5 seconds, but you should look into localStorage
for saving on the client, use onunload
to save to that and recover it later.
I made a html5 notepad that stores to localStorage
every onchange
event and every 500ms and onbeforeunload
to save one field, but it is easy to modify, here's the (essential) code:
<textarea placeholder="Type here, see it here..." id="notepad"></textarea>
<script>
var n = document.getElementById("notepad");
/* save */
var s = function(){localStorage.setItem("notepad", n.value);}
/* retrieve (only on page load) */
if(window.localStorage){ n.value = localStorage.getItem("notepad");}
/* autosave onchange and every 500ms and when you close the window */
n.onchange = s();
setInterval( s, 500);
window.onunload = s();
</script>
if you view source on that page, you'll also find a polyfill used to support older browsers, but localStorage
should work in IE8+
I wouldn't trust the client to complete an ajax call in the onbeforeunload
alone, but I really don't everything about how that function works in the top browsers.