
- HTML Tutorial
- HTML - Home
- HTML - Roadmap
- HTML - Introduction
- HTML - History & Evolution
- HTML - Editors
- HTML - Basic Tags
- HTML - Elements
- HTML - Attributes
- HTML - Headings
- HTML - Paragraphs
- HTML - Fonts
- HTML - Blocks
- HTML - Style Sheet
- HTML - Formatting
- HTML - Quotations
- HTML - Comments
- HTML - Colors
- HTML - Images
- HTML - Image Map
- HTML - Frames
- HTML - Iframes
- HTML - Phrase Elements
- HTML - Code Elements
- HTML - Meta Tags
- HTML - Classes
- HTML - IDs
- HTML - Backgrounds
- HTML Tables
- HTML - Tables
- HTML - Table Headers & Captions
- HTML - Table Styling
- HTML - Table Colgroup
- HTML - Nested Tables
- HTML Lists
- HTML - Lists
- HTML - Unordered Lists
- HTML - Ordered Lists
- HTML - Definition Lists
- HTML Links
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML Color Names & Values
- HTML - Color Names
- HTML - RGB & RGBA Colors
- HTML - HEX Colors
- HTML - HSL & HSLA Colors
- HTML - HSL Color Picker
- HTML Forms
- HTML - Forms
- HTML - Form Attributes
- HTML - Form Control
- HTML - Input Attributes
- HTML Media
- HTML - Video Element
- HTML - Audio Element
- HTML - Embed Multimedia
- HTML Header
- HTML - Head Element
- HTML - Adding Favicon
- HTML - Javascript
- HTML Layouts
- HTML - Layouts
- HTML - Layout Elements
- HTML - Layout using CSS
- HTML - Responsiveness
- HTML - Symbols
- HTML - Emojis
- HTML - Style Guide
- HTML Graphics
- HTML - SVG
- HTML - Canvas
- HTML APIs
- HTML - Geolocation API
- HTML - Drag & Drop API
- HTML - Web Workers API
- HTML - WebSocket
- HTML - Web Storage
- HTML - Server Sent Events
- HTML Miscellaneous
- HTML - Document Object Model (DOM)
- HTML - MathML
- HTML - Microdata
- HTML - IndexedDB
- HTML - Web Messaging
- HTML - Web CORS
- HTML - Web RTC
- HTML Demo
- HTML - Audio Player
- HTML - Video Player
- HTML - Web slide Desk
- HTML Tools
- HTML - Velocity Draw
- HTML - QR Code
- HTML - Modernizer
- HTML - Validation
- HTML - Color Picker
- HTML References
- HTML - Cheat Sheet
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - Character Entities
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
- HTML Resources
- HTML - Quick Guide
- HTML - Useful Resources
- HTML - Color Code Builder
- HTML - Online Editor
HTML - Web Storage
Web Storage
HTML Web storage is a mechanism used for storing structured data on the client side without sending it to the server. These two storage mechanisms are session storage and local storage. Both are collectively part of the HTML5 Web Storage API.
Need of Web Storage
HTML Web storage was introduced to overcome the following drawbacks of cookies:
- Cookies are included with every HTTP request, thereby slowing down your web application by transmitting the same data.
- Cookies are included with every HTTP request, thereby sending data unencrypted over the internet.
- Cookies are limited to about 4 KB of data. Not enough to store required data.
Types of Web Storage
HTML provides two types of web storage:
- Session storage
- Local storage
To use these two web storages (session storage or local storage) in your web application, you can access them through the window.sessionStorage
and window.localStorage
properties, respectively.
The Session Storage
The session storage is temporary, and it gets cleared when the page session ends, which happens when the browser tab or window is closed. The data stored in session storage is specific to each tab or window.
HTML5 introduces the sessionStorage attribute, which would be used by the sites to add data to the session storage, and it will be accessible to any page from the same site opened in that window, i.e., session, and as soon as you close the window, the session would be lost.
Example
Following is the code that would set a session variable and access that variable −
<!DOCTYPE html> <html> <body> <script type="text/javascript"> if( sessionStorage.hits ){ sessionStorage.hits = Number(sessionStorage.hits) +1; } else { sessionStorage.hits = 1; } document.write("Total Hits :" + sessionStorage.hits ); </script> <p>Refresh the page to increase number of hits.</p> <p>Close the window and open it again and check the result.</p> </body> </html>
Local Storage
The local storage is designed for storage that spans multiple windows and lasts beyond the current session. It does not expire and remains in the browser until it is manually deleted by the user or by the web application. In particular, web applications may wish to store megabytes of user data, such as entire user-authored documents or a user's mailbox, on the client side for performance reasons.
Again, cookies do not handle this case well because they are transmitted with every request.
HTML5 introduces the localStorage attribute, which would be used to access a page's local storage area without a time limit, and this local storage will be available whenever you use that page.
Example
Following is the code that would set a local storage variable and access that variable every time this page is accessed, even next time, when you open the window −
<!DOCTYPE html> <html> <body> <script type="text/javascript"> if( localStorage.hits ){ localStorage.hits = Number(localStorage.hits) +1; } else { localStorage.hits = 1; } document.write("Total Hits :" + localStorage.hits ); </script> <p>Refresh the page to increase number of hits.</p> <p>Close the window and open it again and check the result.</p> </body> </html>
Delete Web Storage
Storing sensitive data on a local machine could be dangerous and could leave a security hole. The session storage data would be deleted by the browsers immediately after the session gets terminated.
However, to clear a local storage setting, we need to call localStorage.remove('key'), where 'key' is the key of the value we want to remove. If we want to clear all settings, the localStorage.clear() method can be called.
Example
Following is the code that would clear complete local storage −
<!DOCTYPE html> <html> <body> <script type="text/javascript"> localStorage.clear(); // Reset number of hits. if( localStorage.hits ){ localStorage.hits = Number(localStorage.hits) +1; } else { localStorage.hits = 1; } document.write("Total Hits :" + localStorage.hits ); </script> <p>Refreshing the page would not to increase hit counter.</p> <p>Close the window and open it again and check the result.</p> </body> </html>