轻量级JS Cookie插件js-cookie的使用方法


Posted in Javascript onMarch 22, 2018

Cookie是网站设计者放置在客户端的小文本文件,一般后台语言使用的比较多,可以实现用户个性化的一些需求。js-cookie插件是一个JS操作cookie的插件,源文件只有3.34 KB,非常轻量级。js-cookie也支持npm和Bower安装和管理。下面看看js-cookie的具体用法。

A simple, lightweight JavaScript API for handling cookies

Works in all browsers
Accepts any character
Heavily tested
No dependency
Unobtrusive JSON support
Supports AMD/CommonJS
RFC 6265 compliant
Useful Wiki
Enable custom encoding/decoding
~900 bytes gzipped!

引用方法:

1、引入js-cookie.js

1.直接饮用cdn:<script src="https://cdn.jsdelivr.net/npm/js-cookie@2/src/js.cookie.min.js"></script>

2.本地下载下来后:<script src="/path/to/js.cookie.js"></script>

3.模块化开发时: import Cookies from 'js-cookie'

2、js-cookie.js常用的API和方法

a、设置cookie

Cookies.set('name', 'value', { expires: 7, path: '' });//7天过期

Cookies.set('name', { foo: 'bar' });//设置一个json

b、读取cookie

Cookies.get('name');//获取cookie

Cookies.get(); #读取所有的cookie

c、删除cookie

Cookies.remove('name'); #删除cookie时必须是同一个路径。

下面是国外的介绍

Basic Usage

Create a cookie, valid across the entire site:

Cookies.set('name', 'value');

Create a cookie that expires 7 days from now, valid across the entire site:

Cookies.set('name', 'value', { expires: 7 });

Create an expiring cookie, valid to the path of the current page:

Cookies.set('name', 'value', { expires: 7, path: '' });

Read cookie:

Cookies.get('name'); // => 'value'
Cookies.get('nothing'); // => undefined

Read all visible cookies:

Cookies.get(); // => { name: 'value' }

Delete cookie:

Cookies.remove('name');

Delete a cookie valid to the path of the current page:

Cookies.set('name', 'value', { path: '' });
Cookies.remove('name'); // fail!
Cookies.remove('name', { path: '' }); // removed!

IMPORTANT! When deleting a cookie, you must pass the exact same path and domain attributes that were used to set the cookie, unless you're relying on the default attributes.

Note: Removing a nonexistent cookie does not raise any exception nor return any value.

Namespace conflicts

If there is any danger of a conflict with the namespace Cookies, the noConflict method will allow you to define a new namespace and preserve the original one. This is especially useful when running the script on third party sites e.g. as part of a widget or SDK.

// Assign the js-cookie api to a different variable and restore the original "window.Cookies"

var Cookies2 = Cookies.noConflict();
Cookies2.set('name', 'value');

Note: The .noConflict method is not necessary when using AMD or CommonJS, thus it is not exposed in those environments.

JSON

js-cookie provides unobtrusive JSON storage for cookies.

When creating a cookie you can pass an Array or Object Literal instead of a string in the value. If you do so, js-cookie will store the string representation of the object according to JSON.stringify:

Cookies.set('name', { foo: 'bar' });

When reading a cookie with the default Cookies.get api, you receive the string representation stored in the cookie:

Cookies.get('name'); // => '{"foo":"bar"}'
Cookies.get(); // => { name: '{"foo":"bar"}' }

When reading a cookie with the Cookies.getJSON api, you receive the parsed representation of the string stored in the cookie according to JSON.parse:

Cookies.getJSON('name'); // => { foo: 'bar' }
Cookies.getJSON(); // => { name: { foo: 'bar' } }

Note: To support IE6-7 (and IE 8 compatibility mode) you need to include the JSON-js polyfill: https://github.com/douglascrockford/JSON-js

更多的可以参考官方说明:

https://github.com/js-cookie/js-cookie

https://www.npmjs.com/package/js-cookie

Javascript 相关文章推荐
setInterval 和 setTimeout会产生内存溢出
Feb 15 Javascript
网页前台通过js非法字符过滤代码(骂人的话等等)
May 26 Javascript
JavaScript运行过程中的“预编译阶段”和“执行阶段”
Dec 16 Javascript
jQuery增加与删除table列的方法
Mar 01 Javascript
jQuery Mobile框架中的表单组件基础使用教程
May 17 Javascript
浅谈js中几种实用的跨域方法原理详解
Dec 02 Javascript
jQuery实现大图轮播
Feb 13 Javascript
VueJS 集成 Medium Editor的示例代码 (自定义编辑器按钮)
Aug 24 Javascript
jQuery实现简单的下拉菜单导航功能示例
Dec 07 jQuery
Vue仿今日头条实例详解
Feb 06 Javascript
详细介绍解决vue和jsp结合的方法
Feb 06 Javascript
vue实现动态给id赋值,点击事件获取当前点击的元素的id操作
Nov 09 Javascript
浅谈Webpack 持久化缓存实践
Mar 22 #Javascript
深入浅析Vue.js中 computed和methods不同机制
Mar 22 #Javascript
Java设计中的Builder模式的介绍
Mar 22 #Javascript
使用vue-route 的 beforeEach 实现导航守卫(路由跳转前验证登录)功能
Mar 22 #Javascript
收集前端面试题之url、href、src
Mar 22 #Javascript
vue 的keep-alive缓存功能的实现
Mar 22 #Javascript
bootstrap中selectpicker下拉框使用方法实例
Mar 22 #Javascript
You might like
在IIS上安装PHP4.0正式版
2006/10/09 PHP
PHP IDE phpstorm 常用快捷键
2015/05/18 PHP
DOM精简教程
2006/10/03 Javascript
JavaScript语句可以不以;结尾的烦恼
2007/03/08 Javascript
JavaScript中伪协议 javascript:使用探讨
2014/07/18 Javascript
js实现浏览器窗口大小被改变时触发事件的方法
2015/02/02 Javascript
jQuery选择器源码解读(七):elementMatcher函数
2015/03/31 Javascript
javascript中if和switch,==和===详解
2015/07/30 Javascript
js实现对ajax请求面向对象的封装
2016/01/08 Javascript
简单讲解AngularJS的Routing路由的定义与使用
2016/03/05 Javascript
需灵活掌握的Bootstrap预定义排版类 你精通吗?
2016/06/20 Javascript
利用原生js和jQuery实现单选框的勾选和取消操作的方法
2016/09/04 Javascript
tablesorter.js表格排序使用方法(支持中文排序)
2017/02/10 Javascript
解决Extjs下拉框不显示的问题
2017/06/21 Javascript
React中上传图片到七牛的示例代码
2017/10/10 Javascript
Vue-cli项目获取本地json文件数据的实例
2018/03/07 Javascript
javascript设计模式 ? 观察者模式原理与用法实例分析
2020/04/22 Javascript
[06:42]DOTA2每周TOP10 精彩击杀集锦vol.1
2014/06/25 DOTA
可用于监控 mysql Master Slave 状态的python代码
2013/02/10 Python
Python初学时购物车程序练习实例(推荐)
2017/08/08 Python
Python获取CPU、内存使用率以及网络使用状态代码
2018/02/08 Python
Python关键字及可变参数*args,**kw原理解析
2020/04/04 Python
Jupyter notebook运行Spark+Scala教程
2020/04/10 Python
重写django的model下的objects模型管理器方式
2020/05/15 Python
Python面向对象特殊属性及方法解析
2020/09/16 Python
Parts Express:音频、视频和扬声器的第一来源
2017/04/25 全球购物
英国领先的互联网葡萄酒礼品商:Vintage Wine & Port
2019/05/24 全球购物
出纳工作岗位责任制
2014/02/02 职场文书
党风廉政承诺书
2014/03/27 职场文书
家长给老师的感谢信
2015/01/20 职场文书
现场施工员岗位职责
2015/04/11 职场文书
小学教师教育随笔
2015/08/14 职场文书
python基础入门之字典和集合
2021/06/13 Python
俄罗斯十大城市人口排名,第三首都仅排第六,第二是北方首都
2022/03/20 杂记
MySQL索引 高效获取数据的数据结构
2022/05/02 MySQL
详解flex:1什么意思
2022/07/23 HTML / CSS