The Web Storage API is easy to use as everything can just simply be stored as key value pairs of the localSorage global variable on clients that support the Web Storage API. However depending on the nature of the project it might not always be the best choice when storing large amounts of data. The indexed db option is a better choice when it comes to storage of large amounts of data on the clients computer, and cookie files will always give better backward compatibility when it comes to supporting older browsers but with a limited size.
There is also the idea of using the File reader constructor to have it so the user can save and load files anywhere on their computers local file system. So in some projects the file reader constructor might be be the best choice for handing the saving and loading of state, or other assets in general actually.
Still the Web Storage API is a good option for quickly getting the job done, and most modern browsers support the standard well, any one had to start somewhere when it comes to researching what the options are.
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. Also when saving a value for a local storage api get I will want to always do so in a string format, so there is knowing a thing or two about how to work with the JSON methods. Once a property is set then that value will be there again every page load, site wide, serving the purpose of client side persistence of data on top of other options such as cookie files.
The source code examples for the web storage api that I am writing about in this post can be found in by test vjs Github repository. This post is one of many that is still very much a work in progress, so the latest version of the files as well as some notes that have to do with future plans for editing can be found there.
So then here I have a basic example of the Web Storage API that just stores a single message that can be set from the value property of a text type input element.
I have an event handler that will fire each time a keyboard key is released, or the value changes. So Each time a key is release, or for any reason the value property of the text input element changes I call the set method of my web storage module to set the value for a key that I have set as mess. Then each time the page loads the value of the text element is set to the value in local storage that is obtained by way of the get method of my web storage module.
One major thing about the web storage api is that I can not always count on it being there. There is the question of it not even being supported the browser at all of course, however more often than not these days it could be disabled. So then it would make sense to have some code that will check if the local storage api is even there to begin with, and on top of that do a quick test even if it is there to make sure that it is working as expected. This way I can first test out the API, and if it is working proceed with the normal flow of things, else I can try something else, or at least inform the user that they will not be able to save any progress, settings, data or whatever the case may be.
On top of having a pubic test method that calls the internal test method I also of course have a get and set methods that are also public. These methods also call the private test method each time they are used, and when doing so the call on on disabled call back in the event that a test fails. It is then in this on disabled call back that I could do something like altering the user that local storage is not working and that they might want to do something to fix that.
I will then want to have at least a few demos if this web api library to make sure things work out as expected. For starters I will want to make sure that the test function is working out the way that it should. So then I made a quick simple demo that tests out the test function.
When I run this example by way of the file protocol by just opening up the html file in the browser the web API works. However what is strange is that event when I disable it it still seems to work anyway. I assume that this might have something to do with using the web API by way of the file protocol, or for some reason when I go to disable local storage doing so just seems to not work for whatever reason.
I guess I could see about doing the same when it comes to disabling by way of the http protocol, but I did nit get around to testing that out last time I edited this post at least. However this is something that I would like to look into more at one point or another.
So now that I have covered a basic example of using the web storage API I thought I would write a section on the code that I worked out for the save state system that I am using in one of my canvas examples. The specif example in question is my cross hairs game example that I spent a fair about of time working on a while back.
There is more to working out some kind of save state system for a game, or some kind of game framework. As there is not just simply saving a state to local storage and being done with it. Do not get me wrong move often than not that will work fine, except for when it will not work fine. There is not just using local storage then, but giving a player more than one option for saving a state to the local file system of there computer. I will not be getting into every little detail here when it comes to those sorts of things, but this is still some code that is one of my starting points for making this kind of system for a game.
Although this example is not so much on making use of additional ways of saving data client side, it is however an example that touches base on some additional topics that come up when it comes to making a save state system. Mainly the topic of how to go about coming up with some kind of format for the save state data, when you are working on a game that is not done yet, and you do not know what more you might want to add to the save state data.
Here is a striped down version of the game module for cross hairs that just contains the code that is used to process a save state string. On top of that there is just a method to create a game object. So then I have an object in my game model that contains hard coded default settings, after that I have an array of save string versions that are what the deal is when it comes to the various formates for a game save.
Now to create a simple tool that I can use to edit the save state of a game. So far this tool just edits the damage of a save state that should be there to begin with, but in time I might put more work into this one if I keep working on the cross hairs example.
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 by posting data to it by way of scripting http. However with many of the applications that I have made thus far I do not care to get into that sort of thing if it is the kind of project where I can avoid doing so. With that said when it comes to just some simple game, or application in which there is no sever of web servers to push data back to, then client side storage must be the way to go about doing so.
Another option for saving and loading some data client side would be using the File Reader Constructor. This is a way to prop the user to save a file wherever they want on there local system, and it also allows for them to find a file and open it up as a way to load a state also. This is yet another option on top of something like a password system that is like many old Nintendo games where it is not just a password buy also a state that can be copied and pasted.