首頁 > web前端 > js教程 > 輕量級JS Cookie插件js-cookie的使用方法

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

亚连
發布: 2018-05-26 15:51:21
原創
1984 人瀏覽過

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 &#39;js-cookie&#39;
登入後複製

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
登入後複製

b、讀取cookie

#
Cookies.get(&#39;name&#39;);//获取cookie
Cookies.get(); #读取所有的cookie
登入後複製

c、刪除cookie

Cookies.remove(&#39;name&#39;); 
#删除cookie时必须是同一个路径。
登入後複製

以下是國外的介紹

Basic Usage

Create a cookie, valid across the entire site:

Cookies.set(&#39;name&#39;, &#39;value&#39;);
登入後複製

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

Cookies.set(&#39;name&#39;, &#39;value&#39;, { expires: 7 });
登入後複製

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; });
登入後複製

Cookies.get(&#39;name&#39;); // => &#39;value&#39;
Cookies.get(&#39;nothing&#39;); // => undefined
登入後複製

#Read cookie:

Cookies.get(); // => { name: &#39;value&#39; }
登入後複製

Read all visible cookies:

Cookies.remove(&#39;name&#39;);
登入後複製

Delete cookie:

Cookies.set('name', 'value', { path: '' });
Cookies.remove(&#39;name&#39;); // fail!
Cookies.remove('name', { path: '' }); // removed!
登入後複製

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

var Cookies2 = Cookies.noConflict();
Cookies2.set(&#39;name&#39;, &#39;value&#39;);
登入後複製

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 default attributes.

Note: Removing a nonexistent doas 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 differable v the original "window.Cookies"

Cookies.set(&#39;name&#39;, { foo: &#39;bar&#39; });
登入後複製

Note: The .noConflict method is not necessary when using AMD or CommonJS, thus it is not exposed in those environments

#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.get(&#39;name&#39;); // => &#39;{"foo":"bar"}&#39;
Cookies.get(); // => { name: &#39;{"foo":"bar"}&#39; }
登入後複製

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

Cookies.getJSON(&#39;name&#39;); // => { foo: &#39;bar&#39; }
Cookies.getJSON(); // => { name: { foo: &#39;bar&#39; } }
登入後複製

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:

#rrreee
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驗證使用者名稱和密碼的實例代碼#####################

以上是輕量級JS Cookie插件js-cookie的使用方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板