0% found this document useful (0 votes)
20 views7 pages

HTML Local Storage, Better Than Cookies

Local storage allows web applications to store large amounts of data within the browser that will not be deleted or affect performance. This is an improvement over cookies which were previously used for storage but included in every server request. Local storage can store up to 5MB of data per domain and protocol, does not expire, and is accessible by all pages from the same origin. It provides the localStorage and sessionStorage objects, with localStorage data persisting after the browser closes and sessionStorage data lasting only for the current browser session.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views7 pages

HTML Local Storage, Better Than Cookies

Local storage allows web applications to store large amounts of data within the browser that will not be deleted or affect performance. This is an improvement over cookies which were previously used for storage but included in every server request. Local storage can store up to 5MB of data per domain and protocol, does not expire, and is accessible by all pages from the same origin. It provides the localStorage and sessionStorage objects, with localStorage data persisting after the browser closes and sessionStorage data lasting only for the current browser session.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

HTML5 Local Storage

HTML local storage, better than


cookies.
HTML5 Local Storage
• With local storage, web applications can store data locally
within the user's browser.
• Before HTML5, application data had to be stored in cookies,
included in every server request.
• Local storage is more secure, and large amounts of data can
be stored locally, without affecting website performance.
• Unlike cookies, the storage limit is far larger (at least 5MB)
and information is never transferred to the server.
• Local storage is per origin (per domain and protocol).
• All pages, from one origin, can store and access the same
data.
HTML Local Storage Objects

• HTML local storage provides two objects for


storing data on the client:
1. window.localStorage - stores data with no
expiration date
2. window.sessionStorage - stores data for one
session (data is lost when the browser tab is
closed)
HTML Local Storage Objects

• Before using local storage, check browser


support for localStorage and sessionStorage:
if (typeof(Storage) !== "undefined") {
// Code for localStorage/sessionStorage.
} else {
// Sorry! No Web Storage support..
}
The localStorage Object

• The localStorage object stores the data with no


expiration date.
• The data will not be deleted when the browser is
closed, and will be available the next day, week,
or year.
• // Store
localStorage.setItem("lastname", “Abhigyan");
• // Retrieve
document.getElementById("result").innerHTML
= localStorage.getItem("lastname");
The localStorage Object

if (localStorage.clickcount) {
localStorage.clickcount =
Number(localStorage.clickcount) + 1;
} else {
localStorage.clickcount = 1;
}
document.getElementById("result").innerHTML =
"You have clicked the button " +
localStorage.clickcount + " time(s).";
The sessionStorage Object

• The sessionStorage object is equal to the


localStorage object, except that it stores the
data for only one session.
• The data is deleted when the user closes the
specific browser tab.
• Example

You might also like