设置cookie

  1. function cookie(key, value, options) {
  2. let days
  3. let time
  4. let result
  5. // A key and value were given. Set cookie.
  6. if (arguments.length > 1 && String(value) !== '[object Object]') {
  7. // Enforce object
  8. options = Object.assign({}, options)
  9. if (value === null || value === undefined) {
  10. options.expires = -1
  11. }
  12. if (typeof options.expires === 'number') {
  13. days = options.expires * 24 * 60 * 60 * 1000
  14. time = options.expires = new Date()
  15. time.setTime(time.getTime() + days)
  16. }
  17. value = String(value)
  18. return (document.cookie = `${encodeURIComponent(key)}=${
  19. options.raw ? value : encodeURIComponent(value)
  20. }
  21. ${options.expires ? `; expires=${options.expires.toUTCString()}` : ''}
  22. ${options.path ? `; path=${options.path}` : ''}
  23. ${options.domain ? `; domain='${options.domain}` : ''}
  24. ${options.secure ? '; secure' : ''}`)
  25. }
  26. // Key and possibly options given, get cookie
  27. options = value || {}
  28. const decode = options.raw
  29. ? function(s) {
  30. return s
  31. }
  32. : decodeURIComponent
  33. return (result = new RegExp(`(?:^|; )${encodeURIComponent(key)}=([^;]*)`).exec(document.cookie))
  34. ? decode(result[1])
  35. : null
  36. }

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

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

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. Sublime Text 3 安装包

    摘要 Error while loading PyV8 binary:exit code 3 .sublime-package报错 安装SublimeREPL,可以运行python代码 安装local ...

  2. allegro设置内存分配器的一个坑

    看过<游戏引擎架构>后我开始对内存的分配问题关注,一直想用内存分配器来管理游戏的内存.前两天发现了有许多第三方内存分配器可以用.最后挑中了nedmalloc,这个库也是ogre所使用的,测 ...

  3. GP工作室—Alpha版本发布2

    GP工作室-Alpha版本发布2 一.简介 1.1作业要求 这个作业属于哪个课程 https://edu.cnblogs.com/campus/xnsy/GeographicInformationSc ...

  4. re模块的使用

    re模块下的函数 compile(pattern):创建模式对象 import re pat = re.compile('D') m = pat.search('CBA') #等价于re.search ...

  5. 小程序--->小程序图片上传阿里OSS使用方法

    小程序图片上传阿里OSS使用方法 首先看下参考文档 ( http://blog.csdn.net/qq_38125123/article/details/73870667) 这里只将一些运用过程中遇到 ...

  6. hadoop3.2.0集群搭建的一些坑!

    搭建步骤就不多说了,网上教程很多,这里列举几个: https://blog.csdn.net/pucao_cug/article/details/71698903   2.8版本 https://ww ...

  7. 在Linux环境下设置 ora-01031:insufficient privileges解决方法总结

    今天需要使用sys用户处理问题,但是报错上面ora-01031:insufficient privileges. 在网上有很多方法,这个是自己经过测试的方法步骤. 1:首先检查文件sqlnet.ora ...

  8. vim 实用快捷键

    删除当前行:dd 删除上一行:dj 删除下一行:dk 拷贝当前行:yy 交换当前行和其下一行 交换当前字符和其后的一个字符 剪切当前字符:x 剪切当前光标开始向后三个字符:3x 撤销最近一次修改:u ...

  9. Spring初识、新建工程

    1.spring与三层架构的关系: spring负责管理项目中的所有对象,是一个一站式的框架,容器中的对象决定了spring的功能. 2.spring核心架构 Spring框架主要由六个模块组成,在开 ...

  10. js的三种输出语句,以及html的运行循序

    js最常见的三种输出语句 1.console.log()这个语句是在浏览器控制台输出的.进入网页点击f12可查看 2.alert()弹出一个对话框, 3.document.write这个语句是在页面输 ...