1

So plain and simple, I need a way to save around 50 variables and pass them to the next page to print them out again.

I cannot use a server-side language such as PHP. This must be completely with jQuery/JavaScript.

So to explain the project a little more: I have a large form that will be doing some simple calculations. I need to save all of these variables, including the calculation totals, and create a table on a new page to print them out into so that they are in a nice format. I will then offer two options, to print the table, or to email the table.

There may be a far better alternative than saving the variables and passing them to the next page, although I am just looking for guidance on exactly how to approach this.

I am not asking anyone to write mountains of code for me, just suggestions.

All help and advice is greatly appreciated.

PS. This web app is being created mainly for iOS devices.

Fizzix
  • 23,679
  • 38
  • 110
  • 176
  • Take a look at [Sharing a variable between multiple html pages](http://stackoverflow.com/questions/16264253/sharing-a-variable-between-multiple-html-pages/16264547#16264547) – Xotic750 Sep 29 '13 at 11:04

2 Answers2

5

Cookies or localStorage are the way to go. Preferably localStorage since it has a much better API and can store more data.

According to caniuse.com it's supported in all major browsers. Additionally, the MDN page I linked above contains code that falls back to cookies if localStorage is not available (but of course that restricts the maximum size to whatever limit cookies in that browser have).

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
  • Would this be iOS compatible? Can Cookies or localStorage hold this many variables without any trouble? – Fizzix Sep 29 '13 at 10:57
  • LSO yes, cookies no. It does work on iOS unless the user is in private/incognito mode. – ThiefMaster Sep 29 '13 at 10:58
  • @fizzix it has nothing to do with iOS ... Even Netscape and old browsers like IE4 support cookies. – Ahmad Alfy Sep 29 '13 at 10:59
  • Depending on your scenario, you could save an object literal, instead of 50 variables. That would be easier to understand and to debug too IMO. – roland Sep 29 '13 at 11:03
  • @roland - could you supply an example in the answers section then please? – Fizzix Sep 29 '13 at 11:05
  • @roland: No he can't. Unfortunately LS stores strings and only strings. So unless he JSON-encodes the string first he cannot store an object in there. – ThiefMaster Sep 29 '13 at 11:07
  • @ThiefMaster - really seems like you know what you're talking about. So which one would you recommend for my situation.. Cookies or localStorage? – Fizzix Sep 29 '13 at 11:10
  • 1
    localStorage since it has a decent api – ThiefMaster Sep 29 '13 at 11:11
  • Great, looks like I'll go with localStorage with the Cookie fallback. Thanks for your help! – Fizzix Sep 29 '13 at 11:12
4

iOS supports localStorage, so, simply do :

localStorage.setitem("yourKey", "yourValue");

and

localStorage.getitem('yourKey');
Paul Rad
  • 4,820
  • 1
  • 22
  • 23
  • Seems like the best solution. Is there a way to clear these localStorage variables once the user leaves the page or clicks a button? – Fizzix Sep 29 '13 at 11:02
  • 2
    You can use sessionStorage instead. That will only persist until the browser closes the browser. Both LS and SS also have a `clear()` method. – ThiefMaster Sep 29 '13 at 11:03