Rumah > hujung hadapan web > tutorial js > 轻量级JS Cookie插件js-cookie的使用方法

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

亚连
Lepaskan: 2018-05-26 15:51:21
asal
1982 orang telah melayarinya

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>
Salin selepas log masuk

2.本地下载下来后:

<script src="/path/to/js.cookie.js"></script>
Salin selepas log masuk

3.模块化开发时:

import Cookies from &#39;js-cookie&#39;
Salin selepas log masuk

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

a、设置cookie

Cookies.set(&#39;name&#39;, &#39;value&#39;, { expires: 7, path: &#39;&#39; });//7天过期
Cookies.set(&#39;name&#39;, { foo: &#39;bar&#39; });//设置一个json
Salin selepas log masuk

b、读取cookie

Cookies.get(&#39;name&#39;);//获取cookie
Cookies.get(); #读取所有的cookie
Salin selepas log masuk

c、删除cookie

Cookies.remove(&#39;name&#39;); 
#删除cookie时必须是同一个路径。
Salin selepas log masuk

下面是国外的介绍

Basic Usage

Create a cookie, valid across the entire site:

Cookies.set(&#39;name&#39;, &#39;value&#39;);
Salin selepas log masuk

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

Cookies.set(&#39;name&#39;, &#39;value&#39;, { expires: 7 });
Salin selepas log masuk

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

Cookies.set(&#39;name&#39;, &#39;value&#39;, { expires: 7, path: &#39;&#39; });
Salin selepas log masuk

Read cookie:

Cookies.get(&#39;name&#39;); // => &#39;value&#39;
Cookies.get(&#39;nothing&#39;); // => undefined
Salin selepas log masuk

Read all visible cookies:

Cookies.get(); // => { name: &#39;value&#39; }
Salin selepas log masuk

Delete cookie:

Cookies.remove(&#39;name&#39;);
Salin selepas log masuk

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

Cookies.set('name', 'value', { path: '' });
Cookies.remove(&#39;name&#39;); // fail!
Cookies.remove('name', { path: '' }); // removed!
Salin selepas log masuk

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(&#39;name&#39;, &#39;value&#39;);
Salin selepas log masuk

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(&#39;name&#39;, { foo: &#39;bar&#39; });
Salin selepas log masuk

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

Cookies.get(&#39;name&#39;); // => &#39;{"foo":"bar"}&#39;
Cookies.get(); // => { name: &#39;{"foo":"bar"}&#39; }
Salin selepas log masuk

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(&#39;name&#39;); // => { foo: &#39;bar&#39; }
Cookies.getJSON(); // => { name: { foo: &#39;bar&#39; } }
Salin selepas log masuk

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

上面是我整理给大家的,希望今后会对大家有帮助。

相关文章:

AJAX的原理—如何做到异步和局部刷新

ajax传递多个参数的实现代码

ajax验证用户名和密码的实例代码

Atas ialah kandungan terperinci 轻量级JS Cookie插件js-cookie的使用方法. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan