首页 > 代码库 > Cookie的设置

Cookie的设置

设置使用者名为JJ,保存30天;

<script>

function setCookie(sName,sValue,iDay){

  var oDate=new Date();

  oDate.setDate(oDate.getDate()+iDay);

  document.cookie=sName+"="+sValue+"; expires="+oDate;

}

 

setCookie("user","jj",30);

</script>

 

 

<script>

function setCookie(sName,sValue,iDay){

  var oDate=new Date();

  oDate.setDate(oDate.getDate()+iDay);

  document.cookie=sName+"="+sValue+"; expires="+oDate;

}

 

function getCookie(sName){

  var sCookie=document.cookie;

  

  var arr=sCookie.split("; ");

 

  for(var i=0;i<arr.length;i++){

    var arr2=arr[i].split("=");

    if(arr2[0]==sName){

      return arr2[1];

    }

  }

}

 

setCookie("pass","12345",30);

 

var userValue=http://www.mamicode.com/getCookie("pass");

alert(userValue);

</script>

 

 

 

<script>

function setCookie(sName,sValue,iDay){

  var oDate=new Date();

  oDate.setDate(oDate.getDate()+iDay);

  document.cookie=sName+"="+sValue+"; expires="+oDate;

}

 

function getCookie(sName){

  var sCookie=document.cookie;

  var arr=sCookie.split("; ");

 

  for(var i=0;i<arr.length;i++){

    var arr2=arr[i].split("=");

    if(arr2[0]==sName){

      return arr2[1];

    }

  }

}

 

function removeCookie(sName){

  setCookie(sName,".",-1);

}

</script>