首页 > Web开发 > 详细

js操作获取和设置cookie

时间:2020-01-30 20:08:21      阅读:56      评论:0      收藏:0      [点我收藏+]
/*cookie的三种操作:读取,写入,删除*/
var CookieUtil = {
  get:function(name){
    var cookieName = encodeURIComponent(name) + "=",
        cookieStart = document.cookie.indexOf(cookieName),
        cookieValue = null;
    if (cookieStart > -1) {
      var cookieEnd = document.cookie.indexOf(";",cookieStart);
      if (cookieEnd == -1) {
        cookieEnd = document.cookie.length;
      }
      cookieValue = decodeURIComponent(document.cookie.substring(cookieStart + cookieName.length,cookieEnd));
    }
    return cookieValue;
  },
  set:function(name,value,expires,path,domain,secure){
    var cookieText = encodeURIComponent(name) + "=" + encodeURIComponent(value);
    if (expires instanceof Date) {
      cookieText += ";expires=" + expires.toGMTString();
    }
    if (path) {
      cookieText += ";path=" + path;
    }
    if (domain) {
      cookieText += ";domain=" + domain;
    }
    if (secure) {
      cookieText += ";secure";
    }
    document.cookie = cookieText;
  },
  unset:function(name,path,domain,secure){
    this.set(name,"",new Date(0),path,domain,secure);
  }
};

 

js操作获取和设置cookie

原文:https://www.cnblogs.com/login123/p/12243503.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!