HTML5 localstorage

Dec 9, 2010 · Follow on Twitter and Mastodon javascript

I have spent some time experimenting with the new HTML 5 localStorage feature, which simplifies storing data in browser storage using JavaScript.

For instance, say that you need to uniquely identify a device. The device itself doesn’t send information like the MAC address, phone number etc. so, you’re at a loss.

You can then easily create a unique ID and store it in local storage. The browser can then use the persisted ID to identify itself.

This is all you need to store a small string in local storage:

localStorage.setItem("name", "Daniel");
var name = localStorage.getItem("name");

Local storage does NOT replace a real database, since it’s unique for each browser and can’t hold large amounts of data. It’s however great for smaller pieces of data.

Just note that the user can clear the local storage at any time, or disable it altogether. Do not rely solely on JavaScript for your web app’s functionality.

Discussions & More

Please share any ideas, feedback or comments you may have in the Disqus section below, or by replying on Twitter or Mastodon.

If you found this text interesting, make sure to follow me on Twitter and Mastodon for more content like this, and to be notified when new content is published.

If you like & want to support my work, please consider sponsoring me on GitHub Sponsors.