HTML5 Session Storage

localstoragebefore

In yesterday’s post, I spoke of HTML5 Local Storage. Today, I would like to touch on HTML5 Session Storage. This will be a short post as session storage works a lot like local storage with one very large distinction…the persistence of the data.

With local storage, as you saw in my last post, the data survives to be accessed and utilized at a later browser session. Session storage is just the opposite. Any data that is stored in session storage “dies” after the browser session is complete. So how do we use Session Storage?

We will use the same code that we used for local storage…the HTML…

[code lang=”html”]
<!– HTML to demonstrate HTML5 Local Storage –>
<p>
<!– Button to click that will invoke our counter function. –>
<button onclick="clickCounter()" type="button">Click me!</button>
</p>
<!– Div that will hold the result of clicking the counter button. –>
<div id="result"></div>
<!– Instructions. –>
<p>Click the button to see the counter increase.</p>
<p>Close the browser tab (or window), and try again, and the counter will continue to count (is not reset).</p>
[/code]

Now for the JavaScript…

[code lang=”js”]
// Function demonstrate utilization of session storage
function clickCounter() {
// Test whether browser has HTML5 storage capability
if (typeof (Storage) !== "undefined") {
// Test to see if there is an HTML5 session storage variable key called clickcount
if (sessionStorage.clickcount) {
// Since the storage variable exists, let’s add one to the value and store it back in the session storage variable
// We are running the variable through the number function to ensure it is an integer
sessionStorage.clickcount = Number(sessionStorage.clickcount) + 1;
} else {
// The clickcount session storage variable does not exist, so we create it.
// This should only run the first time we run the program.
sessionStorage.clickcount = 1;
}
// Grab our result div and display the results pulled from session storage
document.getElementById("result").innerHTML = "You have clicked the button " + sessionStorage.clickcount + " time(s).";
} else {
// Display a message if the browser does not support HTML5 storage
document.getElementById("result").innerHTML = "Sorry, your browser does not support web storage…";
}
}
[/code]

Here is the beginning screen shot (it is the same as before)…

localstoragebefore

Here is a screen shot of the clicks in action…

localstorageafter

So, again, the big difference is that the counter data would “die” after closing the browser.

Happy Coding!

Clay Hess

More To Explore

Code

A Quick Tour of the Web Encoding API

Modern web apps live at the boundary between JavaScript strings and raw bytes. The Web Encoding API exists to make that boundary explicit and safe: it lets you encode a string into UTF‑8 bytes and decode bytes back into text. Importantly, these operations aren’t symmetrical—encoding targets UTF‑8, while decoding can interpret UTF‑8 and many legacy encodings. Alongside the synchronous TextEncoder and TextDecoder, the platform also provides stream-based variants for processing text incrementally as data arrives.

Script Proofread And Sentence Grammar Spell Check
Code

EditContext API: A New Foundation for Custom Web Editors

The experimental EditContext API gives developers a new foundation for building custom rich text editors by separating text input and selection from rendering. Instead of relying on contenteditable, you attach an EditContext to a focusable element and manage your own text model, selection state, and UI updates—while still receiving browser-grade events for typing, caret movement, and IME composition. This demo highlights the core event flow and why character bounds matter for accurate input UI, especially in custom-rendered editors.

Share This Post

small_c_popup.png

Need help?

Let's have a chat...


Login

Jump Back In!

Here at Webolution Designs, we love to learn. This includes sharing things we have learned with you. 

Register

Begin Your Learning Journey Today!

Come back inside to continue your learning journey.