亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

輕量級JS Cookie插件js-cookie的使用方法

發布時間:2020-10-09 10:34:51 來源:腳本之家 閱讀:223 作者:mdxy-dxy 欄目:web開發

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

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

门源| 平南县| 米泉市| 镇巴县| 壶关县| 登封市| 凌云县| 昂仁县| 古丈县| 建瓯市| 手机| 堆龙德庆县| 安阳市| 厦门市| 乌兰浩特市| 河间市| 金湖县| 武功县| 永清县| 象州县| 丁青县| 竹北市| 牡丹江市| 安吉县| 株洲县| 新兴县| 当雄县| 前郭尔| 九寨沟县| 沙雅县| 金昌市| 沅江市| 沂南县| 政和县| 镶黄旗| 宽甸| 望谟县| 麻城市| 翁牛特旗| 沾益县| 安达市|