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:<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.模块化开发时: 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
更多的可以参考官方说明:
https://github.com/js-cookie/js-cookie
https://www.npmjs.com/package/js-cookie
问题内容: JPanel和JFrame有什么区别,以及与轻量级,重量级的关系? 问题答案: JPanel是允许将多个UI组件放在一起的容器。JFrame是使用Swing编写的窗口。 所有的Swing组件都是所谓的“轻型”组件,因为它们是用Java编写的。例如,如果您运行Swing应用程序并尝试使用UI分析工具(例如Windows中的WinSpy)对其进行分析,则只会看到一个元素:窗口(JFrame
本文向大家介绍基于zepto的移动端轻量级日期插件--date_picker,包括了基于zepto的移动端轻量级日期插件--date_picker的使用技巧和注意事项,需要的朋友参考一下 前言 做过移动Web开发的同学都知道,移动端日期选择是很常见的需求。在PC端,我们有很丰富的选择,比较出名的就有Mobiscroll和jQuery UI Datepicker。个人看来,这些插件存在的两个显而易见
本文向大家介绍浅谈轻量级js模板引擎simplite,包括了浅谈轻量级js模板引擎simplite的使用技巧和注意事项,需要的朋友参考一下 模板地址:https://github.com/zhangshaolong/simplite欢迎各位提出宝贵意见及贡献代码。特点: 1:代码量少,学习成本低; 2:默认jsp语法标签方式,熟悉jsp的朋友可以直接按照jsp的语法书写模板; 3:使用原生js语法
本文向大家介绍JS使用cookie设置样式的方法,包括了JS使用cookie设置样式的方法的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了JS使用cookie设置样式的方法。分享给大家供大家参考,具体如下: 更多关于JavaScript相关内容感兴趣的读者可查看本站专题:《JavaScript中json操作技巧总结》、《JavaScript切换特效与技巧总结》、《JavaScript查找算
本文向大家介绍js使用cookie记录用户名的方法,包括了js使用cookie记录用户名的方法的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了js使用cookie记录用户名的方法。分享给大家供大家参考,具体如下: cookie思路:当点击登录按钮时,把cookie存起来,再次访问的时候就读取cookie,即把txt的value设置之前存起来cookie的值就行。 希望本文所述对大家Java
本文向大家介绍轻量级javascript 框架Backbone使用指南,包括了轻量级javascript 框架Backbone使用指南的使用技巧和注意事项,需要的朋友参考一下 Backbone 是一款基于模型-视图-控制器 MVC 模式的轻量级javascript 框架 ,可以用来帮助开发人员创建单页Web应用。 借助Backbone 我们可以使用REST的方式来最小化客户端和服务器间的数据传输,