Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8" />
- <title>Localstorage example</title>
- </head>
- <body onload="testLocalStorage()">
- <script>
- function testLocalStorage() {
- let rv1 = localStorage.getItem("receiptValue");
- console.log(rv1); // Will print null, there's no value for this key
- // Set value for the key
- localStorage.setItem("receiptValue", 100);
- let rv2 = localStorage.getItem("receiptValue");
- console.log(rv2); // Will print value
- // Clear the storage
- localStorage.clear();
- let rv3 = localStorage.getItem("receiptValue");
- console.log(rv3); // Will print null, storage is empty
- // To set the complex value, use JSON serialization
- let complexValue = {
- a: 1,
- b: "Some text",
- c: [100, 200, 300],
- d: {
- hello: "world"
- }
- }
- let serialized = JSON.stringify(complexValue); // serialize
- localStorage.setItem("complexData", serialized);
- let deserialized = JSON.parse(localStorage.getItem("complexData")); // deserialize
- console.log(deserialized);
- }
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement