Introduction
- window.localStorage: stores data with no expiration date so the data will persist even if the user closes the tab
- window.sessionStorage: stores data for the one session and the data would be lost when the user closes the tab
localStorage.setItem("lastname", "Singh");
sessionStorage.firstname= "Ratnesh";
- <!DOCTYPE html>
- <html>
- <head>
- <script>
- function SetName()
- {
- if(typeof(Storage) !== "undefined")
- {
- localStorage.setItem("lastname", "Singh");
- sessionStorage.setItem("firstname", "Ratnesh");
- }
- }
- function GetName()
- {
- if(typeof(Storage) !== "undefined")
- {
- document.getElementById("divName").innerHTML = sessionStorage.getItem("firstname")+' ' +localStorage.getItem("lastname");
- }
- }
- function RemoveName()
- {
- if(typeof(Storage) !== "undefined")
- {
- localStorage.clear();
- //localStorage.removeItem("lastname");
- }
- }
- </script>
- </head>
- <body>
- <p><button onclick="SetName()" type="button">Set Name</button>
- <button onclick="GetName()" type="button">GetName</button>
- <button onclick="RemoveName()" type="button">Remove Name</button></p>
- <div id="divName"></div>
- </body>
- </html>

- <!DOCTYPE html>
- <html>
- <head>
- <script>
- function Counter()
- {
- if(typeof(Storage) !== "undefined")
- {
- if (localStorage.clickcount)
- {
- localStorage.clickcount = Number(localStorage.clickcount)+1;
- }
- else
- {
- localStorage.clickcount = 1;
- }
- if (sessionStorage.clickcount)
- {
- sessionStorage.clickcount = Number(sessionStorage.clickcount)+1;
- }
- else
- {
- sessionStorage.clickcount = 1;
- }
- document.getElementById("divLocalStorageResult").innerHTML = "Total hits in all session :" + localStorage.clickcount ;
- document.getElementById("divSessionStorageResult").innerHTML = "Hits in Current session :" + sessionStorage.clickcount ;
- }
- }
- function GetCounter()
- {
- if(typeof(Storage) !== "undefined")
- {
- document.getElementById("divLocalStorageResult").innerHTML = "Total hits in all session :" + localStorage.clickcount ;
- document.getElementById("divSessionStorageResult").innerHTML = "Hits in Current session :" + sessionStorage.clickcount ;
- }
- }
- </script>
- </head>
- <body>
- <p>
- <button onclick="Counter()" type="button">Click me!</button>
- <button onclick="GetCounter()" type="button">Refresh</button>
- </p>
- <div id="divLocalStorageResult"></div>
- <div id="divSessionStorageResult"></div>
- </body>
- </html>
Conclusion
In this article, we studied Web Storage in HTML 5

Gowtham RajamanickamPosted Mar 18, 2015, 3:40 AM
good show...
Gowtham RajamanickamPosted Mar 18, 2015, 3:40 AM
good show..
Rahul Kumar SaxenaPosted Mar 17, 2015, 1:01 AM
good Work..