设置cookie

function cookie(key, value, options) {
let days
let time
let result // A key and value were given. Set cookie.
if (arguments.length > 1 && String(value) !== '[object Object]') {
// Enforce object
options = Object.assign({}, options)
if (value === null || value === undefined) {
options.expires = -1
}
if (typeof options.expires === 'number') {
days = options.expires * 24 * 60 * 60 * 1000
time = options.expires = new Date()
time.setTime(time.getTime() + days)
}
value = String(value) return (document.cookie = `${encodeURIComponent(key)}=${
options.raw ? value : encodeURIComponent(value)
}
${options.expires ? `; expires=${options.expires.toUTCString()}` : ''}
${options.path ? `; path=${options.path}` : ''}
${options.domain ? `; domain='${options.domain}` : ''}
${options.secure ? '; secure' : ''}`)
} // Key and possibly options given, get cookie
options = value || {}
const decode = options.raw
? function(s) {
return s
}
: decodeURIComponent
return (result = new RegExp(`(?:^|; )${encodeURIComponent(key)}=([^;]*)`).exec(document.cookie))
? decode(result[1])
: null
}

/** * getCookie 获取cookies * @param {String} key * @param {String} defultValue */

function getCookie() {
const args = Array.prototype.slice.call(arguments)
const key = args.length > 0 ? args[0] : null
const defaultValue = args.length > 1 ? args[1] : ''
// const cookieValue =cookie(key)
let result = new RegExp(`(?:^|; )${encodeURIComponent(key)}=([^;]*)`).exec(document.cookie)
result = result ? result[1] : null
try {
return result === null ? defaultValue : result
} catch (error) {
throw error
}
}

cookie的设置与取值的更多相关文章

  1. js localStorage 设置和取值

    定义 Storage 对象,对象有get(取值), set(设置), add(加入新值)三个方法 const Storage = {} Storage.get = function (name) { ...

  2. web.config设置和取值

    博客园中有一篇文章对web.config的结构做了很详细的介绍,原文见 http://www.cnblogs.com/gaoweipeng/archive/2009/05/17/1458762.htm ...

  3. jquery对strutrs2 <s:radio>标签的设置和取值

    今天郁闷了1小时. 需求是这样的: <s:radio  list="#{0:'男',1:'女'}" value="member.sex" id=" ...

  4. 封装对Cookie和Session设置或取值的类

    public class CookieHelper : System.Web.SessionState.IReadOnlySessionState    { public static void Se ...

  5. Matlab绘图基础——axis设置坐标轴取值范围

    peaks; axis tight  %Set the axis limits to equal the range of the data  axis square axis 'auto x'  % ...

  6. radio的选中设置以及取值。

    前台:<input type=" id="tg" name="state"/> <a style="cursor:poin ...

  7. MySQL 自增字段取值

    1 前言 本文来自回答思否网友的一个问题,这个网友新建了一张表,auto_increment_increment设为10,AUTO_INCREMENT主键起始值设为9, 当他插入数据的时候,发现主键值 ...

  8. $.cookie()取值设置

    本文为博主原创,未经允许不得转载: 使用jquery.cookie.js中的cookie做了一个折叠式菜单栏,用cookie保存会话的值,其中的值为点击菜单栏时,即在cookie中 保存对应的值,保证 ...

  9. 【JavaScript】JS跨域设置和取Cookie

    cookie 是存储于访问者的计算机中的变量.每当同一台计算机通过浏览器请求某个页面时,就会发送这个 cookie.你可以使用 JavaScript 来创建和取回 cookie 的值.本文主要JS怎样 ...

随机推荐

  1. 第二阶段冲刺个人任务——six

    今日任务: 搭建网络服务器,上传数据库及程序. 昨日成果: 合并程序(统计团队博客).

  2. 18年第一弹射 和网络有关; 艾曲塞嗯诶系列篇 one

    1:当指定接口运行在RIP2组播方式时,以下说法正确的是 2个答案 A 只接收RIPv2组播报文 B  不接收RIPV1 广播报文 2 下面哪条命令是把PPP的认证方式设置为PAP? C ppp au ...

  3. P4452 [国家集训队]航班安排(最大费用最大流)

    P4452 [国家集训队]航班安排 题目传送门 解题思路: 感觉题面让人有很多误解,就是说有k架飞机在0点从0号机场起飞,在t时刻返回机场,给出空载飞行的时间和花费以及m个包机请求的花费和起始时间和终 ...

  4. Python变量、方法、类的命名规则

    1. 变量命名总结: - 1.单下划线开头变量:protected - 2.双下划线开头变量:private - 3.双下划线开头,双下划线结尾:系统内置变量 2. 函数命名总结: - 1.私有方法: ...

  5. 转载---class文件中的字段表集合--field字段在class文件中是怎样组织的

    写的太好了! https://blog.51cto.com/1459294/1932331

  6. 强大的Guava中的新集合类型: Multiset, Multimap, BiMap, Table, ClassToInstanceMap, RangeSet, RangeMap等

    一 Multiset /** * 新类型集合: Multiset: Multiset就是可以保存多个相同的对象,并且无序 * 占据了List和Set之间的一个灰色地带 * 其他实现: TreeMult ...

  7. Informatica在linux下安装搭建

    安装介质清单准备 介质名称 版本信息 描述 Informatica Powercenter 9.5.1 for Linux 64 bit 必须 Java Jdk 1.6.0_45 for Linux ...

  8. .net core 认证与授权(二)

    前言 这篇紧接着一来写的,在第一篇中介绍了认证与授权,同时提出了这套机制其实就是模拟现实中的认证与授权. 同样这篇介绍在这套机制下,用户信息管理机制?这里就会问了,上一篇中认证和授权不是都ok了吗,怎 ...

  9. Web 开发工具类(3): JsonUtils

    JsonUtils 整合了一些对Json的相关操作: package com.evan.common.utils; import java.util.List; import com.fasterxm ...

  10. CBAM(Convolutional Block Attention Module)使用指南

    转自知乎 这货就是基于 SE-Net [5]中的 Squeeze-and-Excitation module 来进行进一步拓展 具体来说,文中把 channel-wise attention 看成是教 ...