jquery获取、设置、删除cookie

时间:2021-01-08 01:10:32

获取cookie:

function getCookie(cname) {
var name = cname + "=";
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);
if (c.indexOf(name) != -1) return c.substring(name.length, c.length);
}
return"";
}

设置cookie:

function setCookie(cname, cvalue, exdays) {//删除cookie
var date = new Date();
date.setTime(date.getTime() + (exdays*24*60*60*1000));
var expires = "expires="+date.toUTCString();
document.cookie = cname + "=" + cvalue + "; " + expires;
};

删除cookie:

function clearCookie(name) {
setCookie(name, "", -1);
}