Saturday, 15 January 2011

How do I set/unset cookie with jQuery? -



How do I set/unset cookie with jQuery? -

how set , unset cookie using jquery, illustration create cookie named test , set value 1?

see plugin:

https://github.com/carhartl/jquery-cookie

you can do:

$.cookie("test", 1);

to delete:

$.removecookie("test");

additionally, set timeout of number of days (10 here) on cookie:

$.cookie("test", 1, { expires : 10 });

if expires alternative omitted, cookie becomes session cookie, , deleted when browser exits.

to cover options:

$.cookie("test", 1, { expires : 10, //expires in 10 days path : '/', //the value of path attribute of cookie //(default: path of page created cookie). domain : 'jquery.com', //the value of domain attribute of cookie //(default: domain of page created cookie). secure : true //if set true secure attribute of cookie //will set , cookie transmission //require secure protocol (defaults false). });

to read value of cookie:

var cookievalue = $.cookie("test");

you may wish specify path parameter if cookie created on different path current one:

var cookievalue = $.cookie("test", { path: '/foo' });

update (april 2015):

as stated in comments below, team worked on original plugin has removed jquery dependency in new project (https://github.com/js-cookie/js-cookie) has same functionality , general syntax jquery version. apparently original plugin isn't going anywhere though.

jquery cookies

No comments:

Post a Comment