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 ?
- Coffee
- Tea
- Milk
Thanks
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 ?
Thanks
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.
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)