輕量級(jí)JS Cookie插件js-cookie的使用方法
Cookie是網(wǎng)站設(shè)計(jì)者放置在客戶端的小文本文件,一般后臺(tái)語言使用的比較多,可以實(shí)現(xiàn)用戶個(gè)性化的一些需求。js-cookie插件是一個(gè)JS操作cookie的插件,源文件只有3.34 KB,非常輕量級(jí)。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.模塊化開發(fā)時(shí): import Cookies from 'js-cookie'
2、js-cookie.js常用的API和方法
a、設(shè)置cookie
Cookies.set('name', 'value', { expires: 7, path: '' });//7天過期
Cookies.set('name', { foo: 'bar' });//設(shè)置一個(gè)json
b、讀取cookie
Cookies.get('name');//獲取cookie
Cookies.get(); #讀取所有的cookie
c、刪除cookie
Cookies.remove('name'); #刪除cookie時(shí)必須是同一個(gè)路徑。
下面是國外的介紹
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
更多的可以參考官方說明:
相關(guān)文章
js style.display=block顯示布局錯(cuò)亂問題的解決方法
下面小編就為大家?guī)硪黄猨s style.display=block顯示布局錯(cuò)亂問題的解決方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-09-09
javascript實(shí)現(xiàn)阻止iOS APP中的鏈接打開Safari瀏覽器
這篇文章主要介紹了javascript實(shí)現(xiàn)阻止iOS APP中的鏈接打開Safari瀏覽器,這個(gè)IOS APP一般是Web APP,否則沒法使用本文的代碼,需要的朋友可以參考下2014-06-06
JavaScript 解析讀取XML文檔 實(shí)例代碼
應(yīng)項(xiàng)目之需求,需用JS操作XML文檔,遂上網(wǎng)查資料,感覺這篇文章還不錯(cuò),特轉(zhuǎn)載到此地,與大家共享!2009-07-07
使用JavaScript校驗(yàn)URL的方法小結(jié)
JavaScript中如何校驗(yàn)一個(gè)URL?最近遇到幾次需要校驗(yàn)URL的,所以本文給大家整理一下幾個(gè)校驗(yàn)URL的方法,文中有詳細(xì)的代碼講解和圖文參考,具有一定的參考價(jià)值,需要的朋友可以參考下2023-12-12
js實(shí)現(xiàn)上下左右鍵盤控制div移動(dòng)
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)上下左右鍵盤控制div移動(dòng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-01-01
用JS中split方法實(shí)現(xiàn)彩色文字背景效果實(shí)例
這篇文章介紹的是利用Javascript中的split方法來實(shí)現(xiàn)彩色文字背景效果,實(shí)現(xiàn)后的效果很好,有需要的可以參考借鑒。2016-08-08

