0

How do i get value of "id" from a session cookie "apple": Decoded below as

"{logo:"Y",id:"5555555555"}"
  1. I want to get value of id ="5555555555" from apple
  2. create another persistent cookie named banana and place this value "id" into it which expires in 10 days.

Pasted My code below:

Var res = $.cookie("apple");

<<Code to split it and get "id">>

$.cookie('id', 'the_value', { expires: 10});

I am new to Jquery and i am trying hard to get the basics . Please help!

Barmar
  • 596,455
  • 48
  • 393
  • 495
Boomer
  • 1
  • 2
  • Might help out with part of your question: http://stackoverflow.com/questions/1458724/how-do-i-set-unset-cookie-with-jquery?rq=1 – mrogers Dec 21 '16 at 22:08
  • If the cookie is signed 'HttpOnly', you can't get it by javascript. – chaoluo Dec 22 '16 at 01:23

2 Answers2

2

Parse the JSON string in the cookie, then get the id property from it. You can then store this in the new cookie.

var obj = JSON.parse(res);
$.cookie('banana', obj.id, { expires: 10 });
Barmar
  • 596,455
  • 48
  • 393
  • 495
0

Check the usage section in the readme here: https://github.com/carhartl/jquery-cookie#usage


Usage

Create session cookie:

$.cookie('name', 'value');

Create expiring cookie, 7 days from then:

$.cookie('name', 'value', { expires: 7 });

Create expiring cookie, valid across entire site:

$.cookie('name', 'value', { expires: 7, path: '/' });

Read cookie:

$.cookie('name'); // => "value"
$.cookie('nothing'); // => undefined

Read all available cookies:

$.cookie(); // => { "name": "value" }

Delete cookie:

// Returns true when cookie was successfully deleted, otherwise false
$.removeCookie('name'); // => true
$.removeCookie('nothing'); // => false

// Need to use the same attributes (path, domain) as what the cookie was written with
$.cookie('name', 'value', { path: '/' });
// This won't work!
$.removeCookie('name'); // => false
// This will work!
$.removeCookie('name', { path: '/' }); // => true

Note: when deleting a cookie, you must pass the exact same path, domain and secure options that were used to set the cookie, unless you're relying on the default options that is.

Blag
  • 5,394
  • 2
  • 18
  • 43