HTML5 Local Storage

HTML5 Local Storage Before

In a previous post, I wrote of the advantages of HTML5 storage. In today’s post, I want to give an example of HTML5 local storage.

First let’s create 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]

So to set the stage, we have an HTML button that once clicked, will invoke a function called clickCounter(). Let’s take a look at what clickCounter() does…

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

In the aforementioned code, we are testing for the availability of HTML5 storage. Next, we are checking to see if our variable exists in local storage. If it does, we add one to the value, otherwise, we create it.

Here are before and after screenshots…

HTML5 Local Storage Before

HTML5 Local Storage After

Further, with HTML5 Local Storage, the variable values are saved from session to session. So if you close your browser, come back in and click the button, the count will pick up from where it last left off.

Happy Coding!

Clay

More To Explore

computer, laptop, work place-2982270.jpg
Code

Unlocking Wireless Communication: A Dive into the Bluetooth API

Wireless communication has become an integral part of our daily lives, and Bluetooth technology is at the forefront of this revolution, enabling devices to exchange data over short distances and creating a world more interconnected than ever before. At the heart of this technology lies the Bluetooth Application Programming Interface (API), a powerful tool for developers looking to harness the capabilities of Bluetooth in their applications. In this blog post, we’ll explore what the Bluetooth API is, how it works, and the possibilities it opens up for innovation in wireless communication.

lighthouse, beacon, atlantic-8578318.jpg
Code

Understanding the Beacon API: Simplifying Asynchronous Data Transfers

In today’s data-driven world, web applications often need to send data back to the server. Traditionally, this has been done using AJAX requests or similar methods. However, these techniques can come with a cost, especially when dealing with data that needs to be sent during the unload phase of a document, such as tracking and diagnostic data. This is where the Beacon API shines by allowing developers to send data to a server more reliably and efficiently.

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.