venerdì 22 marzo 2013

Web Form Autosave [ENG]

Modern browsers support a simple method to store local data across browser (and server) crashes.

The following script autosaves every 10 seconds the form(s) data and - on load - asks if you want to restore the form data.

NOTE: incomplete untested draft! Use at your own risk!

function storeFormFields(inputForm)
{
 for(i=0; i%lt;inputForm.elements.length; i++)
 {
  var element = inputForm.elements[i];
  localStorage.setItem(inputForm.name+"."+element.name, element.value);
 }
}

function loadFormFields(inputForm)
{
 for(i=0; i<inputForm.elements.length; i++)
 {
  var element = inputForm.elements[i];
  var value = localStorage.getItem(inputForm.name+"."+element.name);
  element.value = value;
 }
}

 setInterval(
 function() 
 {
        storeFormFields(propAttForm);
  storeFormFields(ullForm);
  localStorage.setItem("commit_not_done", "true");
    }, 
 10000);
 
function onloadRestore()
{
 if (localStorage.getItem("commit_not_done")=="true")
 {
  if (confirm("Effettuare il recupero dei dati non salvati?"))
  {
   loadFormFields(propAttForm);
   loadFormFields(ullForm);
  }
 }
}

Nessun commento: