Now I mentioned that cookies were the traditional method. There is a newer method. That came with HTML5. That does not mean that cookies are bad and you need to know how to work with them as they are still in wide use. This just means that when it comes to information persistence, we have an easier method…HTML5 Storage.
HTML5 Storage is the ability to save data in name/value pairs in either a local or session scope. If we store some information in the local scope, then it will survive the closing of the browser. If we store in the session scope, then it dies when the browser closes. Let’s look at two examples…
// Store information in HTML5 local storage
localStorage.lastName = "Doe";
// Store information in HTML5 session storage
sessionStorage.firstName = “John”;
In a future post, I am planning on covering HTML5 Storage in greater detail, but suffice it to say that you can access this data using dot object notation. This is a fairly easy method to access the data. I will also cover cookies since those are still on a lot of sites.