Scenario: When building more complex JavaScript applications that run in a user’s browser it’s very useful to be able to store information in the browser so that the information can be shared across different pages and browsing sessions.

Solution: In the recent past, this would have only been possible with cookies – text files saved to a user’s computer – but the way of managing these with JavaScript was not good. Now there is a new technology called local storage that does a similar thing, but with an easier-to-use interface.

Save data to Local Storage:

localStorage.setItem(<<Identifier>>,<<data>>);

 

localStorage.setItem('Url', 'http://techXposer.com');

 

Get data from Local Storage:

localStorage.getItem(<<Identifier>>);

 

var name = localStorage.getItem('Url');

 

Note: Local storage can only be read client-side (i.e., by the browser and your JavaScript). If you want to share some data with a server, cookies may be a better option.

 

Happy Coding… 🙂