Created
April 28, 2009 20:12
-
-
Save halfninja/103370 to your computer and use it in GitHub Desktop.
sessionStorage wrapper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Data stored in DOM Session Storage (on supported browsers). | |
// create the SessionObject with a unique key name, then put data | |
// in to the data property. It will be JSONified to and fro automatically | |
// between page loads. | |
var SessionObject = function(key) { | |
this.data = null; | |
this.key = key; | |
this.store = window.sessionStorage; | |
if (this.store[this.key]) | |
{ | |
this.data = (""+this.store[this.key]).evalJSON(); | |
} | |
Element.observe(window, 'beforeunload', function(){ | |
this.store[this.key] = this.data.toJSON(); | |
}.bind(this)); | |
}; | |
SessionObject.isSupported = function() { | |
return (!!window.sessionStorage); | |
}; | |
//Example | |
var cart = new SessionObject('ShoppingCart'); | |
cart.data = cart.data || []; | |
cart.data.push({id:1234, name:'Sweet Cuppin Cakes'}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment