form_saving.txt
Gesponsorde koppelingen
PHP script bestanden
// --------------------------------------------------
// /FormSaver v2.0
// /Author: Johan Dam. http://johandam.com/
// /Desc: Saves forms using localStorage or sessionStorage.
// --------------------------------------------------
function FormSaver(f, session){
this.form = f;
this.storage = session ? sessionStorage : localStorage;
this.tags = new Array('input','textarea','select');
// Optionele sanity check. Optioneel omdat alle moderne browsers (zelfs IE8) localStorage ondersteunen.
this.check = function(){
try {
return 'localStorage' in window && window['localStorage'] !== null;
} catch(e){
return false;
}
}
this.load = function(){
var input = this.get_input();
for(var i = 0; i < input.length; i ++){
if(!this.storage[this.form + '_' + input.name])
this.storage[this.form + '_' + input.name] = '';
switch(input.type){
case "checkbox":
case "radio":
if(this.storage[this.form + '_' + input.name] == input.value)
input.checked = true;
break;
case 'textarea':
input.innerHTML = this.storage[this.form + '_' + input.name];
break;
case "button":
case "submit":
// Nothing to save here, move along citizen. Dit is om te voorkomen dat ze onder de default komen.
break;
default:
input.value = this.storage[this.form + '_' + input.name];
}
}
}
this.save = function(input){
switch(input.type){
case "textarea":
this.storage[this.form + '_' + input.name] = input.innerHTML;
break;
case "checkbox":
this.storage[this.form + '_' + input.name] = input.checked ? input.value : '';
break;
default:
this.storage[this.form + '_' + input.name] = input.value;
}
}
this.clear = function(){
var input = this.get_input();
for(var i = 0; i < input.length; i ++)
this.storage[this.form + '_' + input.name] = '';
}
this.get_input = function(){
var input = new Array();
for(t in this.tags){
var temp = document.getElementById(this.form).getElementsByTagName(this.tags[t]);
for(i in temp)
input.push(temp)
}
return input;
}
}