HTML | DOM Window localStorage Properties
HTML DOM Window localStorage Properties allow you to store value pairs in web browsers using objects. It is a read-only property. This object is not expired even if the browser is closed. So, the data is never lost.
Syntax:
window.localStorage
localStorage.setItem("key", "value");
var name = localStorage.getItem("key");
localStorage.removeItem("key");
Example-1: Save, Read and Remove data to localStorage
<!DOCTYPE html> <html> <head> <title> HTML | DOM Window localStorage Properties </title> <script> //saving data locally function save() { var fieldValue = document.getElementById('textfield').value; localStorage.setItem('text', fieldValue); } //reading data function get() { var storedValue = localStorage.getItem('text'); if (storedValue) { document.getElementById( 'textfield').value = storedValue; } } //removing stored data function remove() { document.getElementById( 'textfield').value = ''; localStorage.removeItem('text'); } </script> </head> <body onload="get()"> <p1> Type something in the text field and It will Get stored locally by Browser </p1> <input type="text" id="textfield" /> <input type="button" value="save" onclick="save()" /> <input type="button" value="clear" onclick="remove()" /> <p2> when you reopen or reload this page,your text is still there. </p2> <p3> <br>click on clear to remove it.</p3> </body> </html> |
Output:
- Before saving data:

- After saving data:

- Google Chrome
- Internet Explorer
- Firefox
- Opera
- Safari
- HTML | DOM Window frameElement Properties
- HTML | DOM Window defaultStatus Properties
- HTML | DOM Window frames Properties
- HTML | DOM Window opener Properties
- JavaScript | Window innerWidth and innerHeight Properties
- LocalStorage and SessionStorage | Web Storage APIs
- HTML | Window name Property
- HTML | Window top( ) Property
- HTML | Window self( ) Property
- HTML | Window moveBy() Method
- HTML | Window scrollBy() method
- HTML | Window moveTo() Method
- HTML | Window resizeTo() Method
- HTML | Window createPopup() Method
- HTML | Window resizeBy() Method
Example-2: Check storage type and save data.
<!DOCTYPE html> <html> <head> <title> HTML | DOM Window localStorage Properties </title> </head> <body> <div id="SHOW"></div> <script> if (typeof(Storage) !== "undefined") { localStorage.setItem("name", "GEEKSFORGEEKS"); document.getElementById( "SHOW").innerHTML = localStorage.getItem("name"); } else { document.getElementById( "SHOW").innerHTML = "YOUR BROWSER DOES NOT" + "SUPPORT LOCALSTORAGE PROPERTY"; } </script> </body> </html> |
Output:

Supported Browsers: The browser supported by DOM Window localStorage are listed below:
Recommended Posts:
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



