Thursday, May 30, 2013

HTML 5 Web Storage basics

Earlier, when you want to store something in the client browser you can make use of cookies.Cookie is a small piece of data sent from a website and stored in a user's web browser while a user is browsing a website. But now using web storage and javascript it is possible to store arbitrary values in the browser. Web Storage is different from cookies in the sense that it is not shared with the server and is more like a local storage. 

Web storage is of two types namely local and session storage. local storage is something like your database and used for persistent storage of data while session storage is storing data temporarily till your session is over. For example you can store location of a user once he opens the browser. The session is cleared once the windows is closed.This opens a new dimension where the power of server side scripting is brought into client side. This can make browsing more faster as well as increase the security. But as you know there can never be complete security when it comes to the internet.

The API for localstorage and session storage does not vary much and it usually includes setItem(key, value), getItem(key), removeItem(key), clear(), key(index), length.

Now  let us try it out,
  • Open your favourite browser.
  • Open http://www.google.com or simply google :P
  • Open your Javascript Console (for eg: in Chrome  press ctrl+shift+J)
  • Type this: localStorage.name = 'your_name'
  • Note: Javascript is case sensitive
  • Close the browser and open google again
  • Open Javascript console
  • Type console.log(localStorage.name); and your name appears
  • Note: console.log() is just used for printing
  • Now type: delete localStorage.name;
  • and try closing and doing the same process
  • but this time it will be not found since you have deleted the name property
The above functions using the underlying methods as listed above. In the given example setItem is used to set the name property under localstorage.

Powerful isn't it!!!

Stay tuned for more.




1 comments:

  1. My company and I have been using a storage location for the last 8 to 10 years. The staff are real pros. They pay attention to details, are friendly and accommodating.

    ReplyDelete