js封装设置获取cookie

var docCookies = {
  getItem: function (sKey) {
    return decodeURIComponent(document.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*" + encodeURIComponent(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")) || null;
  },
  setItem: function (sKey, sValue, vEnd, sPath, sDomain, bSecure) {
    if (!sKey || /^(?:expires|max\-age|path|domain|secure)$/i.test(sKey)) { return false; }
    var sExpires = "";
    if (vEnd) {
      switch (vEnd.constructor) {
        case Number:
          sExpires = vEnd === Infinity ? "; expires=Fri, 31 Dec 9999 23:59:59 GMT" : "; max-age=" + vEnd;
          break;
        case String:
          sExpires = "; expires=" + vEnd;
          break;
        case Date:
          sExpires = "; expires=" + vEnd.toUTCString();
          break;
      }
    }
    document.cookie = encodeURIComponent(sKey) + "=" + encodeURIComponent(sValue) + sExpires + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "") + (bSecure ? "; secure" : "");
    return true;
  },
  removeItem: function (sKey, sPath, sDomain) {
    if (!sKey || !this.hasItem(sKey)) { return false; }
    document.cookie = encodeURIComponent(sKey) + "=; expires=Thu, 01 Jan 1970 00:00:00 GMT" + ( sDomain ? "; domain=" + sDomain : "") + ( sPath ? "; path=" + sPath : "");
    return true;
  },
  hasItem: function (sKey) {
    return (new RegExp("(?:^|;\\s*)" + encodeURIComponent(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=")).test(document.cookie);
  },
  keys: /* optional method: you can safely remove it! */ function () {
    var aKeys = document.cookie.replace(/((?:^|\s*;)[^\=]+)(?=;|$)|^\s*|\s*(?:\=[^;]*)?(?:\1|$)/g, "").split(/\s*(?:\=[^;]*)?;\s*/);
    for (var nIdx = 0; nIdx < aKeys.length; nIdx++) { aKeys[nIdx] = decodeURIComponent(aKeys[nIdx]); }
    return aKeys;
  }
};

//用法

docCookies.setItem(name, value[, end[, path[, domain[, secure]]]])

原文地址:https://www.cnblogs.com/guolintao/p/9081180.html

时间: 2024-10-29 03:43:18

js封装设置获取cookie的相关文章

js创建和获取cookie

创建cookie document.cookie='like=1'; //创建 cookie键名和值 var str = document.cookie; 获取cookie 读取cookiefunction getCookie(c_name) { if (document.cookie.length>0) { c_start=document.cookie.indexOf(c_name + "=") if (c_start!=-1) { c_start=c_start + c_n

纯js 原生JavaScript 设置 获取 cookie值

function setCookie(name, value, Hours) { var d = new Date(); var offset = 8; var utc = d.getTime() + (d.getTimezoneOffset() * 60000); var nd = utc + (3600000 * offset); var exp = new Date(nd); exp.setTime(exp.getTime() + Hours * 60 * 60 * 1000); docu

设置获取cookie,setCookie,getCookie

设置cookie: function setCookie(name,value) { var Days = 30; var exp = new Date(); exp.setTime(exp.getTime() + Days*24*60*60*1000); document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString(); } 读取cookie: function getCo

js 根据名字获取cookie 的方法

function getcookie(c_name) { if (document.cookie.length > 0) { c_start = document.cookie.indexOf(c_name + "=");//这里因为传进来的的参数就是带引号的字符串,所以c_name可以不用加引号 if (c_start != -1) { c_start = c_start + c_name.length + 1; c_end = document.cookie.indexOf(

js封装Cookie操作 js 获取cookie js 设置cookie js 删除cookie

每天学习一点点 编程PDF电子书.视频教程免费下载:http://www.shitanlife.com/code var CookieUtil = { // 设置cookie set : function (name, value, expires, domain, path, secure) { var cookieText = ""; cookieText += encodeURIComponent(name) + "=" + encodeURIComponen

JS封装cookie操作函数实例(设置、读取、删除)

本文实例讲述了JS封装cookie操作函数.分享给大家供大家参考,具体如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 /*设置cookie*/ function setCookie(name, value, iDay) {   var oDate=new Date();   oDate.setDate(oDate.getDate()+iDay);   document.cook

php设置cookie,在js中如何获取

在php中设置cookie. $email="[email protected]"; setcookie("y_email",$email,time()+3600,"/"); js中获取cookie function getCookie(c_name) { if (document.cookie.length>0) { c_start=document.cookie.indexOf(c_name + "=") if (c

设置cookie、获取cookie、删除cookie函数封装

//设置cookie函数function setCookie(key,value,t){   var oDate = new Date();   oDate.setDate( oDate.getDate() + t );   document.cookie = key + '=' + encodeURI(value) + ';expires=' + oDate.toGMTString();} //获取cookie函数function getCookie(key){ //通过分号空格拆分开所有co

js中的cookie的设置获取和检查

设置cookiefunction setCookie(cname,cvalue,exdays) { var d = new Date(); d.setTime(d.getTime()+(exdays*24*60*60*1000)); var expires = "expires="+d.toGMTString(); ocument.cookie = cname + "=" + cvalue + "; " + expires; } 获取cookie