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

輕量級(jí)JS Cookie插件js-cookie的使用方法

 更新時(shí)間:2018年03月22日 22:03:12   投稿:mdxy-dxy  
js-cookie插件是一個(gè)JS操作cookie的插件,源文件只有3.34 KB,非常輕量級(jí),js-cookie也支持npm和Bower安裝和管理,下面看看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

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

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

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

相關(guān)文章

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

    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瀏覽器

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

    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)URL的方法小結(jié)

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

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

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

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

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

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

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

    js實(shí)現(xiàn)上下左右鍵盤控制div移動(dòng)

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

    JavaScript中的16進(jìn)制字符介紹

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

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

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

最新評論

朝阳县| 建昌县| 肥乡县| 寻甸| 堆龙德庆县| 天全县| 延庆县| 宁强县| 民权县| 桃园市| 佛冈县| 曲麻莱县| 澄迈县| 社旗县| 托克托县| 丰顺县| 汕头市| 浪卡子县| 平果县| 女性| 略阳县| 德江县| 昂仁县| 凭祥市| 醴陵市| 庆元县| 合肥市| 都昌县| 东明县| 通河县| 巴彦淖尔市| 商城县| 文登市| 六盘水市| 六枝特区| 宁安市| 惠州市| 蒙阴县| 偏关县| 深水埗区| 昭苏县|