Cookies are the way to go if you want to save persistent data for the specific user, however you can use HTML5, the difference being that the data stored with HTML is not explicitly shared with the server unless you use HTML5's sessionStorage
. (session storage uses the same API but stores data specifically against the user session and is not persistent beyond session close);
Saving state with the HTML5 API is actually very easy, it is also event driven meaning that an event is fired whenever data is modified allowing you to listen for changes and act accordingly within your javascript / jQuery.
localStorage.name = 'David';
would create a variable within the localStorage called name, with the value David. Close your browser, re-open and console.log the variable, you will notice it persists.
Be wary that localStorage, despite previous documentation will only store strings of data and objects must be serialised and de-serialised if you want to store / retrieve them.
The actual spec is much larger and you can read more about it here:
HTML5 Storage API
Using HTML5 storage
Tips and tricks on using HTML5 storage
If you are not looking for persistant data you can use jQuery's .data()
methods to store arbitrary data against a DOM nodefor later use. This is very useful, certainly if you are developing widgets and need to store non persistent state against a DOM node.
Additional:
ie7 and ie8 support is lacking but you can use a framework like modernizr.js to force legacy browser compatibility with HTML 5.