最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2018年03月22日 22:03:12   投稿:mdxy-dxy  
js-cookie插件是一個JS操作cookie的插件,源文件只有3.34 KB,非常輕量級,js-cookie也支持npm和Bower安裝和管理,下面看看js-cookie的具體用法

Cookie是網(wǎng)站設(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

更多的可以參考官方說明:

https://github.com/js-cookie/js-cookie

https://www.npmjs.com/package/js-cookie

相關(guān)文章

  • js style.display=block顯示布局錯亂問題的解決方法

    js style.display=block顯示布局錯亂問題的解決方法

    下面小編就為大家?guī)硪黄猨s style.display=block顯示布局錯亂問題的解決方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-09-09
  • javascript實現(xiàn)阻止iOS APP中的鏈接打開Safari瀏覽器

    javascript實現(xiàn)阻止iOS APP中的鏈接打開Safari瀏覽器

    這篇文章主要介紹了javascript實現(xiàn)阻止iOS APP中的鏈接打開Safari瀏覽器,這個IOS APP一般是Web APP,否則沒法使用本文的代碼,需要的朋友可以參考下
    2014-06-06
  • JavaScript 解析讀取XML文檔 實例代碼

    JavaScript 解析讀取XML文檔 實例代碼

    應(yīng)項目之需求,需用JS操作XML文檔,遂上網(wǎng)查資料,感覺這篇文章還不錯,特轉(zhuǎn)載到此地,與大家共享!
    2009-07-07
  • 使用JavaScript校驗URL的方法小結(jié)

    使用JavaScript校驗URL的方法小結(jié)

    JavaScript中如何校驗一個URL?最近遇到幾次需要校驗URL的,所以本文給大家整理一下幾個校驗URL的方法,文中有詳細的代碼講解和圖文參考,具有一定的參考價值,需要的朋友可以參考下
    2023-12-12
  • javascript返回頂部效果(自寫代碼)

    javascript返回頂部效果(自寫代碼)

    今天抽空用原生javascript寫了個返回頂部效果,由于本人水平有限,如有問題請指出,在下很樂意接受,有需要的朋友可以參考下
    2013-01-01
  • JS和JQuery實現(xiàn)雪花飄落效果

    JS和JQuery實現(xiàn)雪花飄落效果

    本文主要給大家講述了如何用JS和JQuery兩種方式實現(xiàn)雪花飄落的動畫效果,有需要的朋友收藏一下吧。
    2017-11-11
  • JavaScript?中的行繼續(xù)符操作

    JavaScript?中的行繼續(xù)符操作

    JavaScript?中的字符串操作可能很復(fù)雜,?盡管字符串操作易于掌握,但實施起來卻具有挑戰(zhàn)性,其中一個相關(guān)領(lǐng)域是添加新行,這篇文章主要介紹了JavaScript中的行繼續(xù)符操作,需要的朋友可以參考下
    2023-06-06
  • js實現(xiàn)上下左右鍵盤控制div移動

    js實現(xiàn)上下左右鍵盤控制div移動

    這篇文章主要為大家詳細介紹了js實現(xiàn)上下左右鍵盤控制div移動,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-01-01
  • JavaScript中的16進制字符介紹

    JavaScript中的16進制字符介紹

    最早接觸到\unnn之類的字符是在微軟的官網(wǎng)上。當(dāng)時在網(wǎng)上找了一下這中字符格式,卻不知道該搜什么
    2011-10-10
  • 用JS中split方法實現(xiàn)彩色文字背景效果實例

    用JS中split方法實現(xiàn)彩色文字背景效果實例

    這篇文章介紹的是利用Javascript中的split方法來實現(xiàn)彩色文字背景效果,實現(xiàn)后的效果很好,有需要的可以參考借鑒。
    2016-08-08

最新評論

灵寿县| 武清区| 承德市| 丰县| 双柏县| 涟源市| 灵璧县| 易门县| 固原市| 新兴县| 固阳县| 筠连县| 榆社县| 贺州市| 内江市| 永靖县| 永仁县| 临夏市| 昌宁县| 昭觉县| 新闻| 江阴市| 都江堰市| 紫阳县| 舒兰市| 宜昌市| 灵台县| 察哈| 吉水县| 无锡市| 天津市| 武威市| 嘉荫县| 望都县| 会理县| 上高县| 阿合奇县| 本溪市| 禄丰县| 宜都市| 廊坊市|