栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > JavaScript

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

JavaScript 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

轻量级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:

2.本地下载下来后:

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/81779.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号