How to set a cookie named 'test' and value '1'?
EDIT
especially,how to unset?
How to set a cookie named 'test' and value '1'?
EDIT
especially,how to unset?
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).
});
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
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
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.
Hi Russ,
the good thing about jquery cookie is that it escapes the data