Wednesday, August 29, 2012

Client side data storage

Lots of ways to do it, but I am favoring HTML 5's sessionStorage. Its clean and does not stick around on peoples machines. I hate it when stuff sticks to my machine so I try not to let it stick on others. sessionStorage will expire when the browser closes. Here are a few ways to set it in javascript.

sessionStorage.someKey = 'someValue';

 or if you prefer...

 sessionStorage.setItem('someKey', 'someValue');

 And to retrieve it.

sessionStorage.getItem('someKey')
 or
sessionStorage.someKey

You can display the info with the Document Object Model like so...

document.getElementById("myvalue").innerHTML = sessionStorage.someKey;

You should get this in your HTML

<div id="myvalue">someValue</div>

No comments:

Post a Comment