设置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. (转) exp1-1:// 一次有趣的XSS漏洞挖掘分析(1)

    from http://www.cnblogs.com/hookjoy/p/3503786.html 一次有趣的XSS漏洞挖掘分析(1)   最近认识了个新朋友,天天找我搞XSS.搞了三天,感觉这一套 ...

  2. 关于PDF阅读器

    获取流程 1.点击下载xodo 2.跳转到如下界面,单击箭头所指的版本: 3.单击转到 中国-中文 4.点击获取 5.在跳出来的界面点击红框 6.打开本机的Microsoft Store下载应用 介绍 ...

  3. Ubuntu学习之路2

    由于数据越来越多,学习的也稍微多了点,故将一步一步学习到的命令和快捷键继续到这篇博客. 1. 查看隐藏文件和文件夹 Ctrl+H 2. 打开搜索管理器 Alt+F2 3. 查看系统内存 free -h ...

  4. 解决Idea的Generate Sources无法生成QueryDSL问题

    今天是2020年第一天在家办公,就出现了跟在公司不一样的现象,deploy项目到maven库时失败,之前一直成功. 查到原因在于QueryDSL类没有生成,但为何在公司可以而在家里就不行呢? 鉴于Id ...

  5. VLC for CentOS7

    https://blog.csdn.net/qiuyoujie/article/details/78486947 http://elearning.wsldp.com/pcmagazine/insta ...

  6. rabbitmq使用总结

    rabbitmq 架构图 RabbitMQ 中的 broker 是指什么?cluster 又是指什么 broker 是指一个或多个 erlang node 的逻辑分组,且 node 上运行着 Rabb ...

  7. windows 安装mongodb2

    安装文件:mongodb-win32-x86_64-2008plus-ssl-3.2.6-signed.msi 电脑配置:win7 64位 MongoDB的安装很简单,设置好安装路径后,一直Next直 ...

  8. Message: 'chromedriver' executable needs to be available in the path.

    环境:windows10 python:3.7.3 已经把 executable.exe 添加到了环境变量中,但还是会提示以上错误. 解决办法: from selenium import webdri ...

  9. 用JavaScript完成页面自动操作

    在之前的一篇<JavaScript实现按键精灵>中曾记录了几个事件对象,本文将会对它们进行一次实战,要完成的动作包括滚动.点击和翻页. 一.滚动 滚动是通过修改容器元素的scrollTop ...

  10. springboot使用servlet

    基于注解方式: 基于配置类: