设置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. Redis(四):del/unlink 命令源码解析

    上一篇文章从根本上理解了set/get的处理过程,相当于理解了 增.改.查的过程,现在就差一个删了.本篇我们来看一下删除过程. 对于客户端来说,删除操作无需区分何种数据类型,只管进行 del 操作即可 ...

  2. 美食家App开发日记5

    今天将ListView控件用更强大的Recyclerview控件取代,最后调试了程序. 感觉Android编程难度实在是远远高于javaweb,初次接触,感觉有很多东西想实现,想得很容易,但是实现起来 ...

  3. 【java面试】框架篇之Spring

    1.你如何理解Spring? 具体来说Spring是一个轻量级的容器,用于管理业务相关对象的.核心功能主要为:IOC,AOP,MVC. IOD:控制反转,将对象的创建过程交给容器,让容器管理对象的生命 ...

  4. Qt Installer Framework翻译(7-4)

    组件脚本 对于每个组件,您可以指定一个脚本,来准备要由安装程序执行的操作.脚本格式必须与QJSEngine兼容. 构造 脚本必须包含安装程序在加载脚本时创建的Component对象. 因此,脚本必须至 ...

  5. SpringBoot整合三大组建(Servlet、Listener、Filter)

    >[更多资源和教程请关注公众号:**非科班的科班**.如果觉得我写的还可以请给个赞,谢谢大家,你的鼓励是我创作的动力](https://blog.csdn.net/qq_43255017)## ...

  6. 简述java的ArrayList

    java的ArrayList 基础知识: ArrayList集合长度可以发生改变 泛型 自动装箱和自动拆箱 部分常用的接口方法 boolean add(E obj) E add(int index,E ...

  7. 线程池技术之:ThreadPoolExecutor 源码解析

    java中的所说的线程池,一般都是围绕着 ThreadPoolExecutor 来展开的.其他的实现基本都是基于它,或者模仿它的.所以只要理解 ThreadPoolExecutor, 就相当于完全理解 ...

  8. 017.Python函数匿名函数

    匿名函数 lambda表达式 lambda表达式 : 用一句话来表达只具有返回值的函数,简单,方便,直截了当 # 语法: lambda 参数 : 返回值 无参数的lambda 表达式 def func ...

  9. 【存储类、链接、存储管理】分配内存:malloc()、free()

    一.使用库函数:malloc()分配管理内存 (一)标识符(Identifier) 1. 定义变量时,使用了诸如 a.abc.mn123 这样的名字,它们都是程序员自己起的,一般能够表达出变量的作用, ...

  10. git远程上传文件至github

    一.本地安装和配置git 1.安装git pacman -S git //如果没有问题的话就可以安装成功了 2.验证 git --version //看到结果git version 2.10.2就可以 ...