Skip to content

Instantly share code, notes, and snippets.

@heapwolf
Created March 3, 2011 17:26
Show Gist options
  • Save heapwolf/853134 to your computer and use it in GitHub Desktop.
Save heapwolf/853134 to your computer and use it in GitHub Desktop.
A simplified way to store script state across browser refreshes.
;(function() {
return window.sesh = {
init: function() {
var self = this;
window.addEventListener('unload', function() {
self.serialize();
}, false);
self.parse();
},
data: {},
serialize: function() {
var data = JSON.stringify(this.data);
if(data.length < 2000) {
window.name = data;
}
else {
throw new Error('The value may be too large to store depending on which browsers you plan on targeting.')
}
},
parse: function() {
try {
this.data = JSON.parse(window.name);
}
catch(ex) {}
}
};
})().init();
@indexzero
Copy link

niiice

@heapwolf
Copy link
Author

heapwolf commented Mar 3, 2011

yeah i couldn't find a good lightweight implementation of this, everything was either really antiquated or ivory tower. this is a nice little extensible structure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment