设置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. nuxt.js学习初探

    项目目标 把我个人博客的前端界面部分使用nuxt框架进行服务端渲染 nuxt介绍 nuxt可以把spa根据路由将单页面分割成多页面,比起vue的ssr渲染要更容易使用 nuxt的使用 项目创建 npx ...

  2. PowerCat DNS 隧道通信

    powercat 也是一套基于 DNS 通信协议的工具.Powercat的dns的通信是基于dnscat设计的(其服务端就是dnscat).在使用dnscat时需要进行下载和编译. dnscat服务端 ...

  3. TCP/IP、TCP、UDP、Socket知识汇总

    带你了解TCP/IP,UDP,Socket之间关系 https://blog.csdn.net/chaoshenzhaoxichao/article/details/79785318 主要知识点: T ...

  4. vue3的打包及打包的坑

    1.vue3没有vue.config.js文件,在根目录下建一个vue.config.js文件 2.vue.config.js  3.vue3.3版本前的打包命令  vue3.3版本之后 我用3.3之 ...

  5. 什么是 Trait

    Trait 是从 PHP 5.4 加入的一种细粒度代码复用的语法.以下是官方手册对 Trait 的描述: Trait 是为类似 PHP 的单继承语言而准备的一种代码复用机制.Trait 为了减少单继承 ...

  6. #使用abp框架与vue一步一步写我是月老的小工具(2) 后台搭建初体验

    #使用abp框架与vue一步一步写我是月老的小工具(2) 后台搭建初体验 一.续上前言 关于这个小玩意的产品思考,假设我暂时把他叫我是月老热心人 这是一个没有中心的关系链,每个人进入以后都是以自己为中 ...

  7. 微信小程序框架分析小练手(一)——猫眼电影底部标签导航制作

    旧版猫眼电影底部有4个标签导航:电影.影院.发现.我的,如下图所示: 一.首先,打开微信开发者工具,新建一个项目:movie.如下图: 二.建立如下的一些目录: 三.将底部标签导航图标的素材放到ima ...

  8. OpenCV3入门(五)图像的阈值

    1.图像阈值与二值化 阈值是一种简单的图像分割方法,一幅图像包括目标物体(前景).背景还有噪声,要想从数字图像中直接提取出目标物体,可以设定一个像素值即阈值,然后用图像的每一个像素点和阈值做比较,给出 ...

  9. 70. Climbing Stairs QuestionEditorial Solution

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...

  10. Codeforces_729_C

    http://codeforces.com/problemset/problem/729/C 二分找最小容量,然后找符合的最小花费. #include<iostream> #include ...