Web Storage api in client side javaScript

There are a number of ways to store data client side, but in this post I will be mainly writing about the Web Storage API, rather than index db, cookies files, and many other options for client side persistence of data in a front end javaScript environment.

The Web Storage API is easy to use as everything can just simply be stored as key value pairs of the localSorage global on clients that support the Web Storage API. However depending on the nature of the project it might not always be the best choice. Cookie files will always give better backward compatibility when it comes to older browsers, and the indexed db option is a better choice when it comes to storage of large amounts of data on the clients computer. There is also the idea of using the File reader constructor to have it so the user can save and load files anywhere on there computers file system, which might be a great choice for handing the saving and loading of state, or other assets that way.

Still the Web Storage API is a good option for quickly getting the job done, and most modern browsers support the standard well.

1 - web storage basic example

Working with the Web Storage API is as easy as working with a plain old javaScript object more or less which makes it one of he reasons why I tend to like using it. The localStoarge global can then be used just like that of any other javaScript Object, whatever you want to save on the client just define it as a property of the localStorage global. However there are also setItem and getItem methods that can be used to do get and set properties of the localStoarge global which should be used to do so. Once a property is set then that value will be there again every page load, serving the purpose of client side persistence of data.

So then here I have a basic example of the Web Storage API that just stores a single message that can be set in an test input element. I set some event handlers that will fire each time a keyboard key is released, or the value changes. Each time one of the events fire a set method will set the current value of the text input element to a mess property of the localStorage global. Each time the page loads a get method will fire that will set the value of the text element to the mess property of the localStorage global.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<html>
<head>
<title>web storage</title>
</head>
<body>
<input id="the-text" type="text" placeholder="foo">
<script>
var text = document.getElementById('the-text');
// get an item with local storage
var get = function(){
var mess = localStorage.getItem('mess');
if(mess){
text.value = mess;
}
};
// set an item with local storage
var set = function(e){
localStorage.setItem('mess', e.target.value);
};
text.addEventListener('change', set);
text.addEventListener('keyup', set);
get();
</script>
</body>
</html>

5 - Conclusion

So there are a number of other options when it comes to finding a way to store some data for a user in a web application. Of course there is having a database sever side for example as a way of saving data for a user. However with many of the applications that I have made thus far I do not care to get into that sort of thing f it is the kind of project where I can avoid doing so.