HTMLs Tutorial

HTML Tutorial HTML Tags HTML Basic Tags HTML Attributes HTML Elements HTML Formatting HTML Text Format HTML body tag HTML samp tag HTML script Tag HTML section tag HTML select tag HTML source tag HTML span tag HTML strike tag HTML strong tag HTML style tag HTML sub tag HTML summary tag HTML sup Tag HTML svg tag HTML table tag HTML u tag HTML Headings HTML Paragraphs HTML wbr tag HTML Anchor HTML Image HTML Lists HTML Ordered List HTML Unordered List HTML Form HTML Form input HTML with CSS HTML Layouts HTML References HTML Frames HTML Links Fieldset Tag in HTML Basic HTML Tags Br Tag in HTML Free HTML Templates How to Create a Table in HTML HTML Calendar HTML Card HTML Cellspacing HTML Center Image HTML Checkbox Read-only HTML Cleaner HTML Code for a Tab HTML Comment HTML Compiler HTML Nested Forms HTML Overlay Text on the Image HTML Select Option Default HTML Snake Game HTML Subheader HTML Tab Character dd Tag in HTML How Many HTML Tags are There HTML Align Tag HTML Responsive HTML Tab Code HTML Table Alternate Row Color HTML Table Fix Column Width Contact HTML DL Tag in HTML How to Insert Image in HTML HTML Background Color HTML Dark Mode How to Convert HTML to PNG HTML Data Toggle HTML Email Template HTML Font Color HTML Font Family ID and Class in HTML HTML Tab Space HTML Tab Tag HTML Itemprop HTML Itemscope HTML Form Design HTML Input Only Numbers HTML Textarea HTML to JPG HTML to Markdown Python li Tag in HTML MDN HTML What is the Correct HTML for Making a Hyperlink? What is the Root Element of an HTML Document How to Make a Box in HTML How to Save HTML Files in Notepad How to Align Text in HTML How to Change Font Color in HTML? How to Change Font Size in HTML How to Change Image Size in HTML How to Create a HTML Page How to Create a Link in HTML File? How to Create an HTML File? HR Tag in HTML HTML Base Tag HTML Default Attribute HTML Hyperlink HTML Indent HTML Injection Payloads HTML Input Numbers Only HTML Roadmap HTML Row Height HTML Schedule HTML Space HTML Tab HTML vs HTTP HTML5 API HTML5 Video HTML Collection to Array Text Area in HTML

HTML5 Advance

HTML5 Tutorial HTML5 Tags HTML Button Tag HTML canvas Tag HTML caption Tag HTML City tag HTML Tag HTML5 SVG HTML Event Attribute HTML5 Audio HTML5 Youtube HTML5 Button Tag HTML5 Tags

Misc

How to add JavaScript to HTML How to change font in HTML How to change text color in HTML HTML Date HTML Hide Element HTML Nested Table HTML Reset Button What does HTML stand for? HTML Background Image HTML Tag Div Tag in HTML How to insert Image in HTML How to create a link with no underline in HTML How to insert spacestabs in text using HTMLCSS HTML tag HTML Code HTML Tag HTML Canvas Design a tribute page using HTML and CSS What is a Container tag Font tag in HTML Difference between HTML and DHTML Empty Tag in HTML HTML Button Link Html Line Break Img src HTML Nested List in HTML Placeholder in HTML TD in HTML HTML Space Code HTML Target Attribute HTML Tag Markup Meaning in HTML Border-Collapse in HTML HTML Onclick Online HTML Compiler Convert HTML to PDF HTML Formatter HTML5 - Web Storage HTTP – Responses Container Tag in HTML DL Tag in HTML Horizontal Rule HTML HTML Tab Text Html Table Cell Background Color HTML Table Cell Color HTML Col Width How Many HTML Tags are There Convert String to Unicode Characters in Python HTML Runner HTML Style Attribute HTML Superscript Attribute HTML tabindex Marquee Tag in HTML HTML Dynamic Form HTML side Tag HTML Pattern Attribute HTML q Tag HTML Readonly Base 64 Encoding in HTML Documents Enhancing Data Portability and Security Evo Cam Web Cam HTML Free code camp HTML CSS How to Add a JS File in HTML? How to Add Picture in HTML How to Add the Logo in HTML? How to Add Video in HTML HTML Class Attribute HTML Entities HTML Form Elements HTML Form Templates HTML Marquee Tag HTML Radio Buttons HTML Text box HTML to JSX HTML Tooltip Basic HTML Codes How to Align Image Center in HTML HTML Header Tag HTML Image Tag HTML Next Line

HTML5 - Web Storage

Introduction:

One of the best features of HTML5 is Web Storage. Web applications can store data locally on the client side of the browser with the help of the Web Storage feature. On the browser, it stores data as a key/value pair. DOM storage is another name for web storage.

Although web storage and cookies are similar in how they store data, web storage is faster and more efficient.

HTML5 - Web Storage

Web Storage has the following advantages over cookies:

  • Web Storage can use up to 5MB of storage for each domain. If the space limit has been exceeded, the browser software could notify the user.
  • It is faster than storing cookies because it won't transfer data to the server side.
  • Cookies' data expires after a certain amount of time or session. However, the data stored locally never expires.
  • Cookies are less secure than web storage.

Web Storage Types

There are two web storage types, each with a different lifetime and scope.

Session Storage: Session Storage use Windows.sessionStorage Object that is only kept for the session and lost upon closing the window or browser tab.

Local Storage: Local storages use Windows.localStaorage Object, which is available on all pages. However, data remains unchanged after the browser is stopped and reopened (data is stored without expiration).

  • The sessionStorage Object:

The only difference between the sessionStorage and localStorage objects is that SessionStorage only stores data for a single session. Data will be lost or erased if you close the browser.

<!DOCTYPE html> <html> <head>   <style>     div{       background-color: pink;       height: 50px;     }   </style> </head> <body>   <h2>Example of counter Using Session Storage</h2>   <button onclick="counter();">click me</button>   <div id="output">See the result here:</div>   <script type="text/javascript">     function counter() {      if(sessionStorage.hits){       sessionStorage.hits=Number(sessionStorage.hits)+1; }  else{   sessionStorage.hits=1;  }  document.getElementById('output').innerHTML= "You have clicked counter button for"+ " "+ sessionStorage.hits +" "+"times"; }   </script>  <p>Click the counter button to see the total counts. </p>  <p>Now, if you close the browser then it will reset to initial value. </p> </body> </html>  

Output:

HTML5 - Web Storage

If you click on the "click me" button in the output, it displays that you have clicked the counter button 1 time, as shown below:

HTML5 - Web Storage

If you click on counter button 2 times output will be:

HTML5 - Web Storage

Similarly if you click on counter button 3 three times, the output will be:

HTML5 - Web Storage

If you click on counter button multiple times like shown above, the value will be displayed in the output.

Explanation:

The only difference between the previous example and the local storage counter example is that sessionStorage.hits was used for session storage.

If you close the browser, the counter here will reset to its starting value.

Tips: You can use JavaScript and jQuery to improve the usability of these examples.

  • The localStorage Object:

Storage that covers many windows and extends beyond the current session is designed for use with local storage. Specifically, Web applications may want to store megabytes of user data on the client side for efficiency reasons, such as  entire user-authored documents or a user's mailbox.

Again, the cookies don't handle this situation well because each request passes them across.

Example:

The localStorage property, shown in Example HTML5, can be used to access a page's local storage without a time limit. This local storage is accessible whenever you use the page.

The code that would set a local storage variable and access it each time and this page is accessed including the next time you open the window is as follows.

Example:

<!DOCTYPE html> <html> <head>   <style>     div{       background-color: pink;       height: 50px;     }   </style> </head> <body>   <h2>Example of counter Using Session Storage</h2>   <button onclick="counter();">click me</button>   <div id="output">See the result here:</div>   <script type="text/javascript">     function counter() {      if(sessionStorage.hits){       sessionStorage.hits=Number(sessionStorage.hits)+1; }  else{   sessionStorage.hits=1;  }  document.getElementById('output').innerHTML= "You have clicked counter button for"+ " "+ sessionStorage.hits +" "+"times"; }   </script>  <p>Click the counter button to see the total counts. </p>  <p>Now, if you close the browser then it will reset to initial value. </p> </body> </html>  

Output:

HTML5 - Web Storage
  • Remove Web Storage:

As we've seen, when you close the browser, the data saved in the session storage is automatically removed, but the data saved in local storage remains in the browser.

Therefore, you must use two methods if you want to erase the data from local storage:

localStorage.removeItem('key'): You can use the "key" to remove the value associated with a specific key if you want.

localStorage.clear(): Call the localStorage.clear() function to remove or clear all setting that has a key/value pair.

Example:

<!DOCTYPE html>  <html>  <head>    <title>Web Storage API</title>    <style>       body{        color: green;        text-align: center;        font-size: 30px;        margin-top: 30px;        font-style: italic;        }    </style>  </head>  <body>  <button onclick="remove();">Remove item</button>  <div id="output"></div>    <script>   if(typeof(Storage)!=="undefined") {    localStorage.setItem("name","Bhavya");    localStorage.setItem("Country", "India");    document.getElementById('output').innerHTML= "Hii, my name is"+ " "+ localStorage.name +" "+"and i belongs to"+" "+localStorage.Country;     }    else{    alert("Sorry! your browser is not supporting the browser")   }    function remove() {   localStorage.removeItem("name");     document.getElementById('output').innerHTML= "Hii, my name is"+ " "+ localStorage.name +" "+"and i belongs to"+" "+localStorage.Country;  }  </script>  </body>  </html>  

Output:

HTML5 - Web Storage

If you click on the "Remove item" button, the output will be:

HTML5 - Web Storage

Explanation:

The value for the key "name" will be deleted because we used localStorage.removeItem("name"); in the example above.

Using the localStorage.clear() method, you can either erase the id associated with a specific key or all data.

Conclusion

The term "DOM Storage" is sometimes used equally with "Web Storage." The reason for the latter is that the data is actually kept in the window object of JavaScript (i.e., window.localStorage and window.sessionStorage).

Finally, but just as importantly, remember that users have the ability to disable Web Storage just like they do cookies. Therefore, you must always have a backup plan in place in an emergency.window or sessionStorage.There is no local storage available. Although there may be some issues, like the StorageEvent, most popular web browsers today support HTML5 Web Storage, which is a more convenient, flexible, secure, and faster way to store HTTP state data than HTTP Cookies. Additionally, they follow with the W3C standard and can store data in sessionStorage and localStorage.