輕量級JS Cookie插件js-cookie的使用方法
Cookie是網(wǎng)站設(shè)計(jì)者放置在客戶端的小文本文件,一般后臺語言使用的比較多,可以實(shí)現(xiàn)用戶個性化的一些需求。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.模塊化開發(fā)時: 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è)置一個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
更多的可以參考官方說明:
相關(guān)文章
使用iframe實(shí)現(xiàn)pdf文件預(yù)覽功能
這篇文章主要為大家詳細(xì)介紹了如何使用iframe實(shí)現(xiàn)pdf文件預(yù)覽功能,以及iframe預(yù)覽報(bào)錯問題和iframe未能加載PDF文檔,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-09-09typescript基本數(shù)據(jù)類型HTMLElement與Element區(qū)別
這篇文章主要為大家介紹了typescript基本數(shù)據(jù)類型HTMLElement與Element區(qū)別詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11Javascript動畫的實(shí)現(xiàn)原理淺析
這篇文章主要介紹了Javascript動畫的實(shí)現(xiàn)原理淺析,本文用兩個實(shí)例來解釋Javascript動畫的實(shí)現(xiàn)原理,需要的朋友可以參考下2015-03-03uni-app多環(huán)境配置實(shí)現(xiàn)自動部署的方式詳解
前后端分離開發(fā)模式中,無論前后端都有可能區(qū)分不同的環(huán)境配置,下面這篇文章主要給大家介紹了關(guān)于uni-app多環(huán)境配置實(shí)現(xiàn)自動部署的相關(guān)資料,需要的朋友可以參考下2022-06-06深入理解關(guān)于javascript中apply()和call()方法的區(qū)別
下面小編就為大家?guī)硪黄钊肜斫怅P(guān)于javascript中apply()和call()方法的區(qū)別。小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-04-04