设置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. 无法解析导入javax.mail

    本文链接:https://blog.csdn.net/aaqian1/article/details/89357195下载地址: http://www.oracle.com/technetwork/j ...

  2. idea中当前模块资源目录上显示属于其他模块

    一.错误的2个显示 1.错误显示 src/main目录下的java和resources本来属于cloud-consumer-dept-feign模块 但是在src/main/java却是显示src/m ...

  3. 使用Razor 使用Razor表达式处理命名空间 精通ASP-NET-MVC-5-弗瑞曼

  4. c#数字图像处理(二)彩色图像灰度化,灰度图像二值化

    为加快处理速度,在图像处理算法中,往往需要把彩色图像转换为灰度图像,在灰度图像上得到验证的算法,很容易移植到彩色图像上.24位彩色图像每个像素用3个字节表示,每个字节对应着R.G.B分量的亮度(红.绿 ...

  5. Educational Codeforces Round 81 (Rated for Div. 2) B. Infinite Prefixes

    题目链接:http://codeforces.com/contest/1295/problem/B 题目:给定由0,1组成的字符串s,长度为n,定义t = sssssss.....一个无限长的字符串. ...

  6. 内部类、final与垃圾回收,面试时你一说,面试官就知道

    内部类并不常用,而且使用起来有一定的定式,比如在下面的InnterDemoByTrhead.java里,我们通过内部类的形式创建线程. 1 public class InnerDemoByThread ...

  7. BOM DOM 注意事項

    setTimeout(js,時間)  js处 应该放一个函数 不能放 alert  confirm 等 (否则延时会失效) setTimeout()   和 setInterval() 的区别:    ...

  8. Leetcode 题目整理-4 Longest Common Prefix & Remove Nth Node From End of List

    14. Longest Common Prefix Write a function to find the longest common prefix string amongst an array ...

  9. parzen 窗的matlab实现

    用一下程序简单实现使用parzen窗对正态分布的概率密度估计: (其中核函数选用高斯核) %run for parzen close all;clear all;clc; x=normrnd(0,1, ...

  10. Bash 脚本中的 set -euxo pipefail

    有些开发人员会用Bash来实现很复杂的功能,就像使用别的高级语言一样.他可能觉得自己很牛逼但其他人早就想锤爆他了,Bash的可读性和可维护性远远低于任何高级语言.更要命的是,Bash并没有方便的调试工 ...