首页 > 代码库 > js 操作cookie

js 操作cookie

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 exp = new Date();		exp.setTime(exp.getTime() + expiredays*24*60*60*1000);		document.cookie = name + "="+ value + ";expires=" + exp.toGMTString()+";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;	}

  

js 操作cookie