轻量级JS Cookie插件js-cookie的使用方法
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
更多的可以参考官方说明: