cookie的结构很简单,就是name-value对,用来记录客户端的状态,在访问符合cookie的特征的网站的时候,
会把数据带过去。
cookie可以设置生存时间,根据需要设置不同的数值。如果希望关闭浏览器就cookie结束的话,只需要不设置
该属性就可以了。
js访问cookie:
//set a session only cookie, close browser will wipe off the cookie
function setSessionOnlyCookie(key, value, path, domain) {
document.cookie = key +"="+ value +
( (path)?";path="+path:"" )+ ( (domain)?";domain="+domain: "" );
}
//get a cookie value
function getCookieValue(key) {
var nameEQ = key + "=";
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;
}
Reference: http://www.quirksmode.org/js/cookies.html