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

这次给大家带来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 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

相信看了本文案例你已经掌握了方法,更多精彩请关注【创想鸟】其它相关文章!

推荐阅读:

Angular集成三方UI框架、控件使用详解

Vue文档使用案例总结

以上就是Cookie插件js-cookie使用案例详解的详细内容,更多请关注【创想鸟】其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2763377.html

(0)
上一篇 2025年3月8日 10:10:15
下一篇 2025年3月5日 17:27:27

AD推荐 黄金广告位招租... 更多推荐

相关推荐

  • Vue.js里computed使用案例详解

    这次给大家带来Vue.js里computed使用案例详解,Vue.js里computed使用的注意事项有哪些,下面就是实战案例,一起来看一下。 JS属性: JavaScript有一个特性是 Object.defineProperty ,它能…

    编程技术 2025年3月8日
    200
  • JS中事件委托使用详解

    这次给大家带来JS中事件委托使用详解,JS中事件委托使用的注意事项有哪些,下面就是实战案例,一起来看一下。 事件委托(也叫事件代理),其实这个问题也简单,要想弄明白事件委托,我们先要把事件冒泡的机制搞清楚。举个事件冒泡的例子: 点击 var…

    编程技术 2025年3月8日
    200
  • p5.js实现黄金螺旋动画

    这次给大家带来p5.js实现黄金螺旋动画,p5.js实现黄金螺旋动画的注意事项有哪些,下面就是实战案例,一起来看一下。 效果如下: 主要方法 translate() rotate() arc() 斐波那契螺旋 斐波那契螺旋线也称“黄金螺旋”…

    2025年3月8日
    200
  • 源生JS做出抽奖页面

    这次给大家带来源生JS做出抽奖页面,源生JS做出抽奖页面的注意事项有哪些,下面就是实战案例,一起来看一下。  效果图: 图片素材 : 代码如下,复制即可使用: 幸运抽奖页面 /*CSS代码*/ *{ padding:0; margin:0;…

    2025年3月8日
    200
  • nodejs搭建本地服务器处理跨域

    这次给大家带来nodejs搭建本地服务器处理跨域,nodejs搭建本地服务器处理跨域的注意事项有哪些,下面就是实战案例,一起来看一下。 最近把以前用jquery写的一个小demo拿出来运行的,刚开始的时候忘了开启本地服务导致控制台一直报XM…

    2025年3月8日
    200
  • 使用JS回调函数案例说明

    这次给大家带来使用JS回调函数案例说明,使用JS回调函数的注意事项有哪些,下面就是实战案例,一起来看一下。 在说回调函数之前,不妨先看一段代码,相信有点js基础的同学都能明白他的含义: document.getElementById(‘de…

    编程技术 2025年3月8日
    200
  • JS用事件委托给元素增加事件

    这次给大家带来JS用事件委托给元素增加事件,JS用事件委托给元素增加事件的注意事项有哪些,下面就是实战案例,一起来看一下。 我们在日常开发中有时会通过js创建一些元素,但是如果使用原始的for循环给创建的节点添加事件的方法往往行不通: js…

    编程技术 2025年3月8日
    200
  • JS遍历不规则多维数组方法总结

    这次给大家带来JS遍历不规则多维数组方法总结,JS遍历不规则多维数组的注意事项有哪些,下面就是实战案例,一起来看一下。 直接进入正文: 我们有时候处理数据,可能会遇到一些不规则(无法预料的数据结构),那么拿到这种数据我们如何进行遍历操作呢?…

    2025年3月8日
    200
  • JS+PHP往类动态添加方法

    这次给大家带来JS+PHP往类动态添加方法,JS+PHP往类动态添加方法的注意事项有哪些,下面就是实战案例,一起来看一下。 1.JAVASCRIPT: a. 代码: var a = function(){}var my_a = new a(…

    编程技术 2025年3月8日
    200
  • 用p5.js制作烟花特效的示例代码_javascript技巧

    本篇文章主要介绍了用p5.js制作烟花特效的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧 前言 之前看过一篇文章,使用processing制作烟花特效。效果如下 fireworks 网上调查了一圈了,…

    2025年3月8日
    200

发表回复

登录后才能评论