js-cookie使用步骤详解

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

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

推荐阅读:

JS原始值与引用值有哪些储存方式

Vue如何将过滤器格式化

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

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

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

(0)
上一篇 2025年3月8日 10:35:20
下一篇 2025年1月3日 23:48:31

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

相关推荐

  • url、href、src在JS中的使用

    这次给大家带来url、href、src在JS中的使用,url、href、src在JS中使用的注意事项有哪些,下面就是实战案例,一起来看一下。 一、URL的概念 统一资源定位符(或称统一资源定位器/定位地址、URL地址等,英语:Uniform…

    2025年3月8日 编程技术
    200
  • js验证出生日期正则表达式

    这次给大家带来js验证出生日期正则表达式,实现js验证出生日期正则表达式的注意事项有哪些,下面就是实战案例,一起来看一下。 简言 在表单验证中,经常会用正则表达式做出生日期校验。本文把出生日期分割成几个部分,分步地介绍了实现一个出生日期校验…

    2025年3月8日
    200
  • Vue.js中computed使用详解

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

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

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

    编程技术 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拿出来运行的,刚开始的时候忘了开启本地服务导致控制台一直报…

    2025年3月8日
    200
  • JS回调函数实例详解

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

    编程技术 2025年3月8日
    200
  • JS给动态添加元素增加事件(附代码)

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

    2025年3月8日
    200
  • 需要遍历不规则多维数组时应怎么写JS

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

    2025年3月8日
    200

发表回复

登录后才能评论