HTML local storage, better than cookies.
What is HTML Local Storage?
Using local storage, web applications has the capacity to store data locally inside the user's browser.
Before the launch of HTML5, application data needs to be stored in cookies, which was included in every server request. Local storage is considered to be more secure, and huge amounts of data can be stored locally, without hindering website performance.
The storage limit is far larger (at least 5MB) than cookies and information is not transferred to the server.
Local storage is done per origin (per domain and protocol). All pages, right from one the origin, can store and connect to the same data.
The table numbers specify the first browser version which fully supports Local Storage.
HTML Local Storage Objects
HTML local storage will provide two objects to store data on the client:
Before starting the usage of local storage, checking browser support for localStorage and sessionStorage is necessary:
The localStorage Object
The localStorage object is used to store the data without expiration date. The data is not deleted even after the browser gets closed, and is available the next day, week, or year.
The example above can also be written similar to this:
The syntax for removing the "lastname" localStorage item is given below:
Note: Name/value pairs are always getting stored as strings. You should remember to convert to another format when required!
The following example is used to count the number of times a user is clicking on a button. In this given code the value string gets converted to a number for increasing the counter:
The sessionStorage Object
The sessionStorage object is having equal value similar to the localStorage object, except that it is storing the data for only one session. The data will be deleted when the user is closing the specific browser tab.
The example given below is used to count the number of times a user is clicking a button, in the current session: