Open In App

Web Storage API

Last Updated : 11 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Web API Storage facilitates the feature of storing data within the browser on the client side. It is also referred to as web storage. It uses a key-value pair format to store the data.

Web Storage API Concepts and Usage

The Web Storage API provides two mechanisms for storing data:

  1. sessionStorage:
    • Maintains a separate storage area for each origin during the page session (as long as the browser remains open, including page reloads and restores).
    • Stores data only for the current session, meaning it persists until the browser or tab is closed.
    • Data is never transferred to the server.
    • The storage limit is larger than that of cookies (up to 5MB).
  2. localStorage:
    • Persists even when the browser is closed and reopened.
    • Stores data with no expiration date and gets cleared only through JavaScript or by clearing the browser cache or locally stored data.
    • The storage limit is greater than that of sessionStorage.

Both mechanisms are accessible via the Window.sessionStorage and Window.localStorage properties. Invoking one of these properties creates an instance of the Storage object, which allows you to set, retrieve, and remove data items.

Web Storage API Interfaces

  • Storage: It provides access to a particular local storage.
  • Window: It is a window containing a DOM document.
  • StorageEvent: It is implemented by a storage event which is sent to a window when a storage area changes.

Examples Showing of Web Storage API

1. Using sessionStprage setItem Method:

Example: In this example, we will store the data using the sessionStorage setItem method and retrieve them using the sessionStorage getItem method.

HTML




<!DOCTYPE html>
<html>
 
<head>
    <title>Session storage</title>
</head>
 
<body>
    <center>
        <h1 style="color:green">Welcome To GFG</h1>
        <h2>Geeks details From session Storage</h2>
        <h2 id="ele"></h2>
 
 
    </center>
    <script>
        sessionStorage.setItem('name', 'Arvind Kumar')
        sessionStorage.setItem('Course', 'Mtech');
        sessionStorage.setItem('Age', 23);
        sessionStorage.setItem('Batch', '3rd');
        document.getElementById("ele").innerHTML = '<h3>Name: '
            + sessionStorage.getItem("name") + '</h3><h3>Course: ' +
            sessionStorage.getItem('Course') + '</h3>' + '<h3> Batch: ' +
            sessionStorage.getItem('Batch') + '</h3>' + '<h3>Age: ' +
            sessionStorage.getItem('Age') + '</h3>';
    </script>
</body>
 
</html


Output:

session1

session1

Output of session Storage file:

session11

sessionStorage

2. Using localStorage setItem Method:

Example: In this example we will store the data using localStorage setItem and retrieve them using getItem method.

HTML




<!DOCTYPE html>
<html>
 
<head>
    <title>local storage</title>
</head>
 
<body>
    <center>
 
        <h1 style="color:green">Welcome To GFG</h1>
        <p>
            <label>Username : <input id="name" /></label>
        </p>
        <p>
            <label>Password : <input id="password" /></label>
        </p>
        <p>
            <button onclick="fun()">Submit</button>
        </p>
        <p>
            <button onclick="fun2()">Get details</button>
        </p>
        <h2 id="ele"></h2>
 
 
    </center>
    <script>
        function fun() {
            localStorage.setItem('name',
                document.getElementById('name').value)
            localStorage.setItem('password',
                document.getElementById('password').value);
        }
        function fun2() {
            document.getElementById("ele").innerHTML = '<h3>name: '
                + localStorage.getItem("name") + '</h3><h3>Passward: ' +
                localStorage.getItem('password') + '</h3>';
        }
    </script>
</body>
 
</html>


Output:

local2

local2

Output of local storage:

local1

In summary, the Web Storage API simplifies data storage and retrieval, making it a valuable tool for web developers. Remember to use it wisely and consider the storage limits and security implications.

Supported Browsers:

  • Chrome
  • Edge
  • Firefox
  • Safari
  • Opera


Similar Reads

How to make Auto-filling Forms with jQuery and Web Storage API ?
The Web Storage API provides mechanisms by which browsers can store key/value pairs, in a much more intuitive fashion than using cookies. The two mechanisms within Web Storage are sessionStorage and localStorage, session stores data only for a session that is until the browser is closed, local storage does the same thing but persists even when the
5 min read
How to clear session storage data with specified session storage item ?
In this article, we are going to learn how we can clear the session storage of a browser using JavaScript by getting the specified session storage item. We can achieve this by using Window sessionStorage( ) property. The Window sessionStorage() property is used for saving key/value pairs in a web browser. It stores the key/value pairs in a browser
2 min read
Image and Video Storage with Azure Blob Storage: Media Applications
"Binary Large Object-Based Storage (BLOB)" stands for Binary Large Object. It is a storage type where files of any size and type can be stored, ranging from gigabytes to terabytes to petabytes and beyond. This seemingly infinite storage is unstructured and is distinct from databases or data warehouses. Types Of Azure Blob StorageBlock Blob: Used fo
11 min read
Optimizing Costs with Azure Blob Storage: Storage Tiers and Lifecycle Policies
Azure Blob Storage, a key component of Microsoft Azure, is a powerful service designed to store and manage massive amounts of unstructured data in the cloud. It offers various ways to optimize costs, including selecting appropriate storage tiers and implementing lifecycle policies. This article explores these cost optimization strategies to help bu
5 min read
Managing Local Storage & Session Storage using React Hooks
To manage Loacal Storage and Session Storage, we can use hooks like useEffect and useState provided by React. Managing local or session storage is a repetitive task, so it is a good practice to create a custom hook that manages the storage properly. In this article, I'll cover the whole process of managing storage with the best industry standard pr
3 min read
Difference Between Local Storage, Session Storage And Cookies
The HTTP protocol is one of the most important protocols for smooth communication between the server and the client. The main disadvantage of the HTTP protocol is that it is a stateless protocol, which means it does not track any kind of response or request by the server or the client. So in order to resolve this problem, there are three ways to tr
6 min read
Differences between Web Services and Web API
Web Services: A Web services are any bit of services that makes it accessible over the Internet and normalizes its correspondence through XML encoding. A customer conjures web services by sending a solicitation (for the most part as an XML message), and the services send back an XML response. Web services summon communication over a network, with H
3 min read
Uploading and Downloading Objects in Google Cloud Storage: Command Line and API
Cloud storage refers to storing your data in remote locations i.e., the cloud. Cloud Storage became increasingly popular in the information era. With tons of data in hand, storing them in the cloud is very affordable. By storing the data in the cloud, we can access the data at any time irrespective of the device and the location. Google Cloud Stora
7 min read
Difference Between Web 1.0, Web 2.0, and Web 3.0
Web 1.0 was all about fetching, and reading information. Web 2.0 is all about reading, writing, creating, and interacting with the end user. It was famously called the participative social web. Web 3.0 is the third generation of the World Wide Web, and is a vision of a decentralized web which is currently a work in progress. It is all about reading
8 min read
LocalStorage and SessionStorage | Web Storage APIs
SessionStorage and LocalStorage are known as the web storage API. Data can be stored on the client side by using these APIs. SessionStorage: SessionStorage is used for storing data on the client side.Maximum limit of data saving in SessionStorage is about 5 MB.Data in the SessionStorage exist till the current tab is open if we close the current tab
4 min read