轻量级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 相关文章推荐
arguments对象
Nov 20 Javascript
提升你网站水平的jQuery插件集合推荐
Apr 19 Javascript
Js+Flash实现访问剪切板操作
Nov 20 Javascript
基于javascript 闭包基础分享
Jul 10 Javascript
JS组件系列之使用HTML标签的data属性初始化JS组件
Sep 14 Javascript
js利用clipboardData实现截屏粘贴功能
Oct 12 Javascript
vue双向绑定的简单实现
Dec 22 Javascript
jQuery插件HighCharts实现的2D对数饼图效果示例【附demo源码下载】
Mar 09 Javascript
微信小程序实现图片上传功能实例(前端+PHP后端)
Jan 10 Javascript
实用Javascript调试技巧分享(小结)
Jun 18 Javascript
Vue.js页面中有多个input搜索框如何实现防抖操作
Nov 04 Javascript
JavaScript交换两个变量方法实例
Nov 25 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
编写自己的php扩展函数
2006/10/09 PHP
PHP SPL标准库之接口(Interface)详解
2015/05/11 PHP
php for 循环使用的简单实例
2016/06/02 PHP
Jquery 类网页微信二维码图块滚动效果具体实现
2013/10/14 Javascript
jquery 淡入淡出效果的简单实现
2014/02/07 Javascript
javascript实现浏览器窗口传递参数的方法
2014/09/03 Javascript
Nodejs异步回调的优雅处理方法
2014/09/25 NodeJs
javascript Deferred和递归次数限制实例
2014/10/21 Javascript
JavaScrip常见的一些算法总结
2015/12/28 Javascript
JS遍历数组及打印数组实例分析
2016/01/21 Javascript
解决js函数闭包内存泄露问题的办法
2016/01/25 Javascript
JavaScript 隐性类型转换步骤浅析
2018/03/15 Javascript
jQuery 实现批量提交表格多行数据的方法
2018/08/09 jQuery
vue.js表单验证插件(vee-validate)的使用教程详解
2019/05/23 Javascript
微信小程序实现类似微信点击语音播放效果
2020/03/30 Javascript
微信小程序实现打开并下载服务器上面的pdf文件到手机
2019/09/20 Javascript
webpack3升级到webpack4遇到问题总结
2019/09/30 Javascript
vue实现多级菜单效果
2019/10/19 Javascript
微信小程序wx.request的简单封装
2019/11/13 Javascript
vue实现div可拖动位置也可改变盒子大小的原理
2020/09/16 Javascript
Python入门_条件控制(详解)
2017/05/16 Python
Python简单实现两个任意字符串乘积的方法示例
2018/04/12 Python
使用pip发布Python程序的方法步骤
2018/10/11 Python
python+os根据文件名自动生成文本
2019/03/21 Python
Python3 实现串口两进程同时读写
2019/06/12 Python
利用python计算时间差(返回天数)
2019/09/07 Python
浅谈Python中文件夹和python package包的区别
2020/06/01 Python
pandas之分组groupby()的使用整理与总结
2020/06/18 Python
Python 实现PS滤镜的旋涡特效
2020/12/03 Python
python中remove函数的踩坑记录
2021/01/04 Python
一款css实现的鼠标经过按钮的特效
2014/09/11 HTML / CSS
利用CSS3实现的文字定时向上滚动
2016/08/29 HTML / CSS
教师思想作风整顿个人剖析材料
2014/10/10 职场文书
大学生国家助学金感谢信
2015/01/23 职场文书
小学教师求职信范文
2015/03/20 职场文书
Python基于百度API识别并提取图片中文字
2021/06/27 Python