I'm building an HTML5 / Javascript / jQuery application for personal use. The application needs to literally just store two numbers in order to be able to function. What is the absolute lightest way I can store two numbers between script executions and page reloads? Ideally, the solution would not require the application to run on a server.
Light solution for storing persistent, local data for a Javascript application
226 Views Asked by Nathan Arthur At
3
There are 3 best solutions below
0

Local storage is a good idea depending on your browser, here's a very good write up: http://diveintohtml5.info/storage.html
localStorage
would be easiest:and to retrieve:
Note that
localStorage
can only store strings, hence the+
operator in that last line to cast the stored value back into a number. If you want to store objects, useJSON.stringify
andJSON.parse
.Whether to use the direct properties or
setItem
style access is mostly a matter of personal choice. That said, in theory the methods are more rigorous and recommended as they avoid any possibility of confusing the built-in properties of thelocalStorage
object with the keys that you're trying to store. Also, the methods can be shimmed on older browsers but direct property access cannot.