tags:

views:

1547

answers:

6

How to set a cookie named 'test' and value '1'?

EDIT

especially,how to unset?

+13  A: 

See the plugin:

http://plugins.jquery.com/project/cookie

You can then do:

$.cookie("test", 1);

To delete:

$.cookie("test", null);

Additionally, to set a timeout of a certain number of days (10 here) on the cookie:

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

If the expires option is omitted, then the cookie becomes a session cookie, and is deleted when the browser exists.

To cover all the options:

$.cookie("test", 1, {
   expires : 10,           //expires in 10 days

   path    : '/',          //The value of the path atribute of the cookie 
                           //(default: path of page that created the cookie).

   domain  : 'jquery.com'  //The value of the domain attribute of the cookie
                           //(default: domain of page that created the cookie).

   secure  : true          //If set to true the secure attribute of the cookie
                           //will be set and the cookie transmission will
                           //require a secure protocol (defaults to false).
});
Kazar
How to unset a cookie?
Shore
I've added it to answer
Kazar
+1  A: 

You need to use a plugin available here..

http://plugins.jquery.com/project/cookie

and then to write a cookie do $.cookie("test", 1);

to access the set cookie do $.cookie("test");

Raj

Raj
+2  A: 

No need to use jQuery particularly to manipulate cookies.

From QuirksMode

function createCookie(name,value,days) {
if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}

function eraseCookie(name) {
createCookie(name,"",-1);
}

Take a look at

Russ Cam
A: 

visit thissite

vinoth
A: 

Hi,

has anyone tried to set a cookie for the whole domain and it's subdomains ?

According to: http://code.google.com/p/cookies/wiki/Documentation

this should be the way to do it however it didn't work for me. domain: '*.mydomain.com',

any ideas ?

Update: it seems that the link above is created by another author. Also it seems my domain key wasn't passed as a parameter of the cookie.

lordspace
This is a question, not an answer. Click the "Ask Question" link below the search box to submit a new question.
TheMagician
This reads like a *question*, or a *point of discussion*. As such it's currently out of place, I'd suggest opening a proper question, as SO isn't a forum for branching discussions. I welcome the question, but...y'know, etiquette... =)
David Thomas
A: 

Hi Russ,

the good thing about jquery cookie is that it escapes the data

lordspace