1.判断cookie存不存在
var existence = document.cookie.indexOf('ticket='); if(existence > -1){//存在 } else if(existence == -1){ //不存在 }
2.设置cookie
kk_main.setCooKie = function(name,value,expiredays){ var doMain = kk_main.WEB_URL.substring(kk_main.WEB_URL.indexOf('.')); var cookieStr = name + "="+ value; if(expiredays > 0){ //等于0时,关闭浏览器自动清除Cookies. var exp = new Date(); exp.setTime(exp.getTime() + expiredays*24*60*60*1000); cookieStr += ";expires=" + exp.toGMTString(); } document.cookie = cookieStr +";path=/;Domain="+ doMain; }
3.获取cookie
kk_main.getCookie = function(name){ var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)"); if(arr=document.cookie.match(reg)) return unescape(arr[2]); else return null; }