0

How do I store this ordered list into a cookie so that I can view it in another page ? What should be the syntax of the target page ?

  1. Coffee
  2. Tea
  3. Milk

Thanks

TropicalViking
  • 407
  • 2
  • 9
  • 25
  • Possible duplicate of [How to pass JavaScript variables to PHP?](http://stackoverflow.com/questions/1917576/how-to-pass-javascript-variables-to-php) – Dave Mar 10 '16 at 20:55

2 Answers2

3

For example, you can use the jQuery.cookie plugin to manage your cookies.

var list = ['coffee', 'tea', 'milk'];
$.cookie('cookie_name', JSON.stringify(list));

To get the values and transform in an array, you have to do that:

var list = JSON.parse($.cookie('cookie_name'));

Hope it helps.

Mario Araque
  • 4,562
  • 3
  • 15
  • 25
1

You can get and set cookie values in JavaScript with document.cookie:

document.cookie = "list=,Coffee,Tea,Milk";

Here I set the data with the key of "list". To access value you will need to parse the cookie for the key "list". Cookies only store string values so you will need to turn it into an array as well:

var cookieValue = document.cookie.replace(/(?:(?:^|.*;\s*)list\s*\=\s*([^;]*).*$)|^.*$/, "$1");
var list = cookieValue.split(",");
console.log(list)