cookie

落爺英雄遲暮 提交于 2020-04-07 16:44:10

1:添加cookie

document.cookie =  " user = name ";

document.cookie = " pass = 123456 ";

2: 设置过期时间:expires = 时间

var oDate = new Date();

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

3:设置cookie

<script>
  var oForm = document.getElementById("oForm");
  var oUser = document.getElementById("oUser");
  function setCookie(name, value, iDay) {
    var oDate = new Date();
    oDate.setDate(oDate.getDate() + iDay);
    document.cookie = name + "=" + value + ";expires =" + oDate;
  }

  function getCookie(name) {
    var arr = document.cookie.split(";");
    for(var i = 0; i < arr.length; i++){
      var arr2 = arr[i].split("=");
      if(arr2[0] == name){
      return arr2[1];
      }
    }
    return "";
  }
  function removeCookie(name){
    setCookie(name, 1, -1);
  }
  window.onload = function(){
    oForm.onsubmit = function(){
    setCookie("useName", oUser.value, 14);
  }
    oUser.value = getCookie("useName");
  }
</script>

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!