//添加cookie:cookie名称,cookie值,以及在多少小时后过期。这里约定expireHours为0时不设定过期时间,即当浏览器关闭时cookie自动消失 function addcookie(name, value, expireHours) { var cookieString = name + "=" + escape(value); //判断是否设置过期时间 if (expireHours > 0) { var date = new Date(); date.setTime(date.getTime + expireHours * 3600 * 1000); cookieString = cookieString + "; expire=" + date.toGMTString(); } document.cookie = cookieString; } //设置cookie function setCookie(c_name, value, expiredays) { var exdate = new Date(); exdate.setDate(exdate.getDate() + expiredays); document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString()); } //获取指定名称的cookie值:该函数返回名称为name的cookie值,如果不存在则返回空 function getcookie(name) { var strcookie = document.cookie; var arrcookie = strcookie.split("; "); for (var i = 0; i < arrcookie.length; i++) { var arr = arrcookie[i].split("="); if (arr[0] == name) return arr[1]; } return ""; } //删除cookie function delCookie(name){ var exp = new Date(); exp.setTime(exp.getTime() - 1); var cval = getCookie(name); if (cval != null) document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString(); }