2

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.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Nathan Arthur
  • 8,287
  • 7
  • 55
  • 80

3 Answers3

6

localStorage would be easiest:

localStorage.setItem('a', 1);   // or localStorage.a = 1
localStorage.setItem('b', 2);

and to retrieve:

var a = +localStorage.getItem('a');  // or just a = +localStorage.a

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, use JSON.stringify and JSON.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 the localStorage object with the keys that you're trying to store. Also, the methods can be shimmed on older browsers but direct property access cannot.

Alnitak
  • 334,560
  • 70
  • 407
  • 495
  • Beware localstorage not supported by IE7. Have a look here http://caniuse.com/#search=localstorage – Akshay Khandelwal Sep 13 '13 at 18:26
  • 2
    Beware, shim available on [**MDN**](https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Storage) – adeneo Sep 13 '13 at 18:26
  • 1
    @AkshayKhandelwal the OP said HTML5, thus ruling out IE7/8 – Alnitak Sep 13 '13 at 18:27
  • Yes, tis true. And it'll be only for my personal use, so I can afford to worry less about compatability. But thank you for the warnings, nonetheless; and thank you, @Alnitak, for the concise and extremely helpful answer! I'll be trying this solution first. +1! – Nathan Arthur Sep 13 '13 at 18:37
  • Thanks again, Alnitak, for the great answer! It's the one I used, and works like a charm. Accepted. – Nathan Arthur Sep 15 '13 at 16:00
1

Local storage is a good idea depending on your browser, here's a very good write up: http://diveintohtml5.info/storage.html

Ahmed Nuaman
  • 12,662
  • 15
  • 55
  • 87
1

Plan old cookies will work as well. To set a cookie:

document.cookie = "MyNumber=1; expires=Fri, 3 Aug 2001 20:47:11 UTC; path=/";

To traverse the cookies:

var cookies = document.cookie.split(";"), i;
for(i = 0; i < cookies.length; i++)
{
  console.log(cookies[i]);
}
Ryan
  • 14,392
  • 8
  • 62
  • 102
  • +1 for giving a concise and useful example of cookie use! I just may go this route should I decide later that I want higher compatibility. – Nathan Arthur Sep 13 '13 at 18:37
  • 1
    @NathanArthur If you want higher compatibility, use the shims that adeneo linked to in my answer, that use cookies as an alternate storage but still preserve the `localStorage` API. – Alnitak Sep 13 '13 at 18:44
  • Great idea, @Alnitak. I just might do that. – Nathan Arthur Sep 13 '13 at 18:49