Skip to content

Instantly share code, notes, and snippets.

@JmPotato
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JmPotato/346db0a04d7a5069d0ce to your computer and use it in GitHub Desktop.
Save JmPotato/346db0a04d7a5069d0ce to your computer and use it in GitHub Desktop.
JavaScript AutoSave
function AutoSave(it) {
var _value = it.value;
if (_value) {
var expDays = 30;
var exp = new Date();
exp.setTime( exp.getTime() + (expDays * 86400000) );
var expires='; expires=' + exp.toGMTString();
document.cookie = "AutoSaveContent=" + escape (_value) + expires;
}
}
function AutoLoad(it) {
var _value = it.value;
if (!_value) {
var _LastContent = GetCookie('AutoSaveContent');
if (!_LastContent) return;
if (confirm("Load Last AutoSave Content?")) {
it.value = _LastContent;
return true;
}
}
}
function getCookieVal (offset) {
var endstr=document.cookie.indexOf (";",offset);
if (endstr==-1) endstr=document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}
function GetCookie (name){
var arg=name+"=";
var alen=arg.length;
var clen=document.cookie.length;
var i = 0;
while (i<clen){
var j=i+alen;
if (document.cookie.substring(i,j)==arg) return getCookieVal (j);
i = document.cookie.indexOf(" ",i)+1;
if (i==0) break;
}
return null;
}
function DeleteCookie (name) {
var exp=new Date();
exp.setTime (exp.getTime()-1);
var cval=GetCookie (name);
document.cookie=name+"="+cval+";expires="+exp.toGMTString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment