Save form when user refreshes page from browser or click on any anchor tag inside page I have made a form in drupal.

What I want is when the browser unload the page from any click whether by clicking on anchor tag or from browser refresh buttons I want to save data of my form through js/ajax or anything possible in drupal.

If any more explanation needed please do ask me. Thanks in advance.

I was trying do this by browser 'unload' event and 'beforeunload' event. But unable to save data of my form because browser doesn't wait for anything writtent inside and just ask for confirmation. Any help will appreciated.

This what I was trying to do:

$(window).bind('beforeunload', function() {
  return 'Do you really want to leave?';
  //coming here instead of it what i want
  //is save the form then return if user says yes and no the don't save the form.
});
You would need to create your own AJAX implementation to achieve this. Upon some trigger, whenever you want that to be, (I would make it save any time a field looses focus), you would perform an AJAX call saving the information into a database. When the user returns, provided they are traceable in some way either through their UID or a valid session, you would fill the form out again with the information from the temporary table in the database. When the user completes the form, let Drupal write the data accordingly. Otherwise, there is no way to save the users information and present it to them again. This method, if the user is registered, would always work. Make sure you add a "clear form" button though so they can clear it if they want.

Update: You could use .unload