View source for the script.



If you want to save persistent data for the user and don't wish to use a server side language, or wish to use cookies, then this storage class will do it all for you. The data is persistent, so when you restart your browser the data can be recalled.

This will work in Firefox 2 and Internet Explorer 6.0+, and what ever other browser supports the globalStorage object.

How to use

It's very easy to use. It's recommended that the code above goes into your head tags.

Next you need to create a new instance of the class, at the same time you need to create a new storage database by passing a unique name. So in this example I will create a variable called "name" and store an instance of the Storage class in it...

var name = new Storage("firstname_db");

So now we have our instance, we have access to 2 methods...

obj.save(value); obj.load();

So what we will do below is save my first name...

name.save("Peter");

So now we have something saved, it can be what you like, could be a preference on your site like skin option, or expand / collapse a div. All we need to do is get our value. So lets say that on the index page of our site we saved the users name, and on another page of the site we want to retrieve the users name, to do that we create an instance of the Storage class and called the "load" method...

var name = new Storage("firstname_db");

alert("Hello " + name.load());

The user would then get a message saying "Hello [Name Here]".

Remember, the class can be reused more than once, so you can have multiple instances of the Storage class on the same page.