首頁  >  文章  >  web前端  >  Cookie插件js-cookie使用案例詳解

Cookie插件js-cookie使用案例詳解

php中世界最好的语言
php中世界最好的语言原創
2018-05-09 09:46:341908瀏覽

這次帶給大家Cookie外掛js-cookie使用案例詳解,Cookie外掛程式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:2.本地下載下來後:

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 doany noted raise doany ##ise doany

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.

// 將 js-cookie api 指派給不同的變數並還原原始的「window.Cookies」

##var Cookies2 = Cookies.noConflict();

Cookies2.set(' name ', 'value');

注意:使用AMD 或CommonJS 時不需要.noConflict 方法,因此它不會在這些環境中公開。

##js-cookie 為cookie 提供不顯眼的JSON 儲存。如果這樣做,js-cookie 將根據 JSON.stringify 儲存物件的字串表示形式:

#Cookies.set('name', { foo: 'bar' });

##當使用預設的Cookies.get api 讀取cookie 時,您會收到儲存在cookie 中的字串表示形式:

#
Cookies.get('name'); // => '{"foo":"bar"}'
Cookies.get(); // => { name: '{"foo":"bar"}' }

當使用Cookies.getJSON api 讀取cookie 時,您會收到根據JSON.parse 儲存在cookie 中的字串的解析表示:

Cookies.getJSON('name'); // => { foo: 'bar' }
Cookies.getJSON(); // => { name: { foo: 'bar' } }

注意:要支援IE6-7(和IE 8 相容模式),您需要包含JSON-js 填入:https :// github.com/douglascrockford/JSON-js相信看過本文案例你已經掌握了方法,更多精彩請關注php中文網其他相關文章!

Angular整合三方UI框架、控制項使用詳解

Vue文件使用案例總結

#

以上是Cookie插件js-cookie使用案例詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn