轻量级JS Cookie插件js-cookie的使用方法

js-cookie插件是一个js操作cookie的插件,源文件只有3.34 kb,非常轻量级,js-cookie也支持npm和bower安装和管理,下面看看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');//获取cookieCookies.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

上面是我整理给大家的,希望今后会对大家有帮助。

相关文章:

AJAX的原理—如何做到异步和局部刷新

ajax传递多个参数的实现代码

ajax验证用户名和密码的实例代码

以上就是轻量级JS Cookie插件js-cookie的使用方法的详细内容,更多请关注【创想鸟】其它相关文章!

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

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

(0)
上一篇 2025年3月8日 07:23:48
下一篇 2025年3月7日 22:12:01

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

相关推荐

  • 深入浅析Vue.js中 computed和methods不同机制

    这篇文章给大家介绍了vue.js中 computed和methods不同机制,在vue.js中,methods和computed两种方式来动态当作方法使用,文中还给大家提到了computed和methods的区别,感兴趣的朋友一起看看吧 在…

    编程技术 2025年3月8日
    200
  • 如何使用Vue+Nuxt.js 实现服务端渲染

    这次给大家带来如何使用Vue+Nuxt.js 实现服务端渲染,使用Vue+Nuxt.js 实现服务端渲染的注意事项有哪些,下面就是实战案例,一起来看一下。 直接使用 Vue 构建前端单页面应用,页面源码时只有简单的几行 html,这并不利于…

    2025年3月8日 编程技术
    200
  • JavaScript分步实现一个出生日期的正则表达式

    本文把出生日期分割成几个部分,分步地介绍了实现一个出生日期校验的完整过程。对出生日期正则表达式感兴趣的朋友参考下吧 简言 在表单验证中,经常会用正则表达式做出生日期校验。本文把出生日期分割成几个部分,分步地介绍了实现一个出生日期校验的完整过…

    2025年3月8日
    200
  • Vue.js中的computed工作原理

    这篇文章,我们通过实现一个简单版的和vue中computed具有相同功能的函数来了解computed是如何工作的。对vue.js中的computed工作原理感兴趣的朋友一起学习吧 JS属性: JavaScript有一个特性是 Object.…

    编程技术 2025年3月8日
    200
  • 基于JavaScript实现幸运抽奖页面

    这篇文章主要为大家详细介绍了基于javascript实现幸运抽奖页面,具有一定的参考价值,感兴趣的小伙伴们可以参考一下  JS实现简单的幸运抽奖页面,供大家参考,具体内容如下  效果图: 图片素材 : 代码如下,复制即可使用: 立即学习“J…

    2025年3月8日
    200
  • JS中的回调函数实例浅析

    这篇文章主要介绍了js中的回调函数,结合实例形式简单分析了javascript回调函数的感念、功能、使用方法及相关注意事项,需要的朋友可以参考下 本文实例讲述了JS中的回调函数。分享给大家供大家参考,具体如下: 在说回调函数之前,不妨先看一…

    编程技术 2025年3月8日
    200
  • JS实现为动态添加的元素增加事件功能示例

    这篇文章主要介绍了js实现为动态添加的元素增加事件功能,结合实例形式分析了javascript基于事件委托实现针对动态添加的元素增加事件的相关操作技巧,需要的朋友可以参考下 本文实例讲述了JS实现为动态添加的元素增加事件功能。分享给大家供大…

    2025年3月8日
    200
  • JS实现遍历不规则多维数组的方法

    这篇文章主要介绍了js实现遍历不规则多维数组的方法,涉及javascript数组递归遍历相关实现与使用技巧,需要的朋友可以参考下 本文实例讲述了JS实现遍历不规则多维数组的方法。分享给大家供大家参考,具体如下: 直接进入正文: 我们有时候处…

    2025年3月8日
    200
  • 使用Vue.js和Flask来构建一个单页的App的示例

    本篇文章主要介绍了使用vue.js和flask来构建一个单页的app的示例,我觉得挺不错的,现在分享给大家,也给大家做个参考。一起过来看看吧 在这个教程中,我们将讲解如何将vue.js单页应用与Flask后端进行连接。 一般来说,如果你只是…

    2025年3月8日 编程技术
    200
  • JS代码实现电脑配置检测功能

    这篇文章主要介绍了js代码实现电脑配置检测功能,需要的朋友可以参考下 下面一段代码给大家分享js代码实现电脑配置检测功能,具体代码如下所示: nbsp;html>               h1 { text-align: cent…

    2025年3月8日
    200

发表回复

登录后才能评论