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

 更新时间:2018年03月22日 22:03:12   投稿:mdxy-dxy  
js-cookie插件是一个JS操作cookie的插件,源文件只有3.34 KB,非常轻量级,js-cookie也支持npm和Bower安装和管理,下面看看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

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

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

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

相关文章

  • js style.display=block显示布局错乱问题的解决方法

    js style.display=block显示布局错乱问题的解决方法

    下面小编就为大家带来一篇js style.display=block显示布局错乱问题的解决方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-09-09
  • javascript实现阻止iOS APP中的链接打开Safari浏览器

    javascript实现阻止iOS APP中的链接打开Safari浏览器

    这篇文章主要介绍了javascript实现阻止iOS APP中的链接打开Safari浏览器,这个IOS APP一般是Web APP,否则没法使用本文的代码,需要的朋友可以参考下
    2014-06-06
  • JavaScript 解析读取XML文档 实例代码

    JavaScript 解析读取XML文档 实例代码

    应项目之需求,需用JS操作XML文档,遂上网查资料,感觉这篇文章还不错,特转载到此地,与大家共享!
    2009-07-07
  • 使用JavaScript校验URL的方法小结

    使用JavaScript校验URL的方法小结

    JavaScript中如何校验一个URL?最近遇到几次需要校验URL的,所以本文给大家整理一下几个校验URL的方法,文中有详细的代码讲解和图文参考,具有一定的参考价值,需要的朋友可以参考下
    2023-12-12
  • javascript返回顶部效果(自写代码)

    javascript返回顶部效果(自写代码)

    今天抽空用原生javascript写了个返回顶部效果,由于本人水平有限,如有问题请指出,在下很乐意接受,有需要的朋友可以参考下
    2013-01-01
  • JS和JQuery实现雪花飘落效果

    JS和JQuery实现雪花飘落效果

    本文主要给大家讲述了如何用JS和JQuery两种方式实现雪花飘落的动画效果,有需要的朋友收藏一下吧。
    2017-11-11
  • JavaScript 中的行继续符操作

    JavaScript 中的行继续符操作

    JavaScript 中的字符串操作可能很复杂, 尽管字符串操作易于掌握,但实施起来却具有挑战性,其中一个相关领域是添加新行,这篇文章主要介绍了JavaScript中的行继续符操作,需要的朋友可以参考下
    2023-06-06
  • js实现上下左右键盘控制div移动

    js实现上下左右键盘控制div移动

    这篇文章主要为大家详细介绍了js实现上下左右键盘控制div移动,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-01-01
  • JavaScript中的16进制字符介绍

    JavaScript中的16进制字符介绍

    最早接触到\unnn之类的字符是在微软的官网上。当时在网上找了一下这中字符格式,却不知道该搜什么
    2011-10-10
  • 用JS中split方法实现彩色文字背景效果实例

    用JS中split方法实现彩色文字背景效果实例

    这篇文章介绍的是利用Javascript中的split方法来实现彩色文字背景效果,实现后的效果很好,有需要的可以参考借鉴。
    2016-08-08

最新评论