Saving information locally on a user's computer is a powerful strategy for a developer who creates something for the Internet.
if you are working in a web application, then you have two types of web storage that help store and retrieve data in the client’s browser . these two repositories
1. Local storage
2. Session storage
Both stores support a file containing data for a unique identifier for the URL as an object of a pair of key values. you can use any localStorage () or sessionStorage () storage for this.
a unique URL identifier is created for each user’s browser, so when you try to get data from these repositories, you need this unique URL to get the data. if you do not want to use this unique URL, you can also implement your own combination to create the URL.
These repositories are used for different purposes and have their own functions.
when you store your data in localStorage , you can access it anytime you return after closing the browser by following this unique URL.
In the other hand, when you use sessionStorage to store and retrieve data, you can only access data when you open the browser. when you close the browser, it will clear all your data. and when you return, you will find that nothing exists on this URL. and
Now, an example for storing and accessing data from localstorage.
var car = {}; car.wheels = 4; car.doors = 4; car.sound = 'Boss'; car.name = 'Audi R8'; localStorage.setItem( 'car', JSON.stringify(car) ); console.log( JSON.parse( localStorage.getItem( 'car' ) ) );
when you try to save and access data, then following the url.
if(localStorage && localStorage.getItem('car')){ console.log(JSON.parse(localStorage.getItem('car'))); }else{ $.getJSON("http://query.yahooapis.com/xyz?user1",function(data){ if(localStorage){ localStorage.setItem('car',JSON.stringify(data)); } console.log(data); }); }
I hope this gives you an idea of working with web repositories. for more information about storing and accessing data from web storage. Click here..