js设置cookie

//设置cookie
  function setCookie(){
  	//获取当前时间
	var date=new Date();
	var expireDays=10;
	//将date设置为10天以后的时间
	date.setTime(date.getTime()+expireDays*24*3600*1000);
	document.cookie ="loginName="+$("#loginName").val()+";expires="+date.toGMTString();
  }
  //获取cookie
  function getCookie(){
  	var cookiestr = document.cookie;
  	var cooks = cookiestr.split(";");
  	var loginName = "";
  	for(var i=0;i<cooks.length;i++){
  		var cs = cooks[i].split("=");
  		if(cs[0]=="loginName"){
  			loginName = cs[1];
  			break;
  		}
  	}
  	$("#loginName").val(loginName);
  }
 

你可能感兴趣的:(cookie)