Short browser flows often need to remember a choice after a reload without keeping it indefinitely. sessionStorage lets JavaScript save small string values for the current tab, such as a checkout step.
The Storage methods work with key-value pairs. setItem() creates or replaces a value under a key, while getItem() returns the saved string or null when that key is missing. Objects and arrays need conversion to strings before storage.
Saved values belong to the page's origin and tab, survive reloads, and are cleared when the tab's session ends. They suit temporary interface state that can be recreated, rather than durable records or a way to share changes between tabs.
Steps to save a sessionStorage value with JavaScript:
- Create a file named
index.htmlin a new folder using your text editor.- index.html
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Checkout session state</title> <style> body { font: 20px/1.5 system-ui, sans-serif; margin: 32px; } select { font: inherit; } </style> </head> <body> <h1>Checkout session state</h1> <label for="flow-step">Checkout step</label> <select id="flow-step"> <option value="cart">Cart</option> <option value="shipping">Shipping</option> <option value="review">Review</option> </select> <p id="flow-status">No step saved in this tab.</p> <script> </script> </body> </html>
- Insert the saved-step restoration code between the
<script>and</script>tags inindex.html.const sessionKey = "sg:checkout-step"; const flowStep = document.querySelector("#flow-step"); const status = document.querySelector("#flow-status"); const savedStep = sessionStorage.getItem(sessionKey); if (savedStep !== null) { flowStep.value = savedStep; status.textContent = `Restored step: ${savedStep}`; }
The script sits after the controls so they exist when it runs. A missing key leaves Cart selected and the initial status unchanged.
- Append the change handler immediately after the restoration code, before
</script>.flowStep.addEventListener("change", () => { sessionStorage.setItem(sessionKey, flowStep.value); status.textContent = `Saved step: ${flowStep.value}`; });
The selection supplies a string such as
review. Selecting another step replaces the value undersg:checkout-stepwithout changing other keys.Any script running on the same origin can read this storage, including injected scripts. Passwords, access tokens, and sensitive personal data do not belong in sessionStorage.
- Save
index.htmlwith both JavaScript blocks inside the script element. - Start a local web server from the folder containing
index.html.$ python3 -m http.server 8000 --bind 127.0.0.1
This command requires Python 3 and serves the folder only on your computer. Storage needs a web origin; opening the file directly with a file: URL can raise a SecurityError. Browser policies that block storage can also prevent saving.
- Open
http://127.0.0.1:8000/in a browser tab.
- Choose Review from the Checkout step list.

- Reload the same tab with the browser's Reload button.
Before reloading, the status reads
Saved step: review. The handler displays it only after setItem() returns. - Confirm that Review remains selected with the status
Restored step: review.
The changed status comes from getItem() during page startup. If the list returns to Cart or the status stays
No step saved in this tab., the saved value was not restored. The address must keep the same scheme, hostname, and port.