For everyone who is interested, I finished creating a "localstorage with expirations" script here, http://plugins.jquery.com/project/localcache
What I am doing: creating an extension for Storage so that the user can do this:
localStorage.setThing(key, value)
and the user can do the following:
localStorage.setThing("key1", 1) localStorage.setThing("key2", "this is a string") localStorage.setThing("key3", { prop1: "this is a json obj" })
In my setThing method, I check the typeof value for the value, and if typeof value == "object" , I save it as localStorage.setItem(key, JSON.stringify(value))
In the getThing method, I know that the value that makes it in localStorage will always be a string. So how can I do this?
var val = localStorage.getItem("key3") if (val is a previously JSON.stringify'd object) // <-- ?? return JSON.parse(val)
Do I need to do regular expression validation on val, and if so, does anyone have a pattern that tells me if the string is really a JSON.stringify'd object?
Thanks!
source share