当前位置:网站首页>js操作Cookie,js设置Cookie值,js读取Cookie值
js操作Cookie,js设置Cookie值,js读取Cookie值
2022-06-21 06:50:00 【技匠而已】
js对Cookie的操作
前言:
这里简要介绍使用java对Cookie进行操作的方法, 但并不建议使用Cookie,因为有些用户会禁用网站使用的Cookie,如果使用不当会导致用户无法使用网站,所以对Cookie进行简要介绍,了解就好。
js函数使用方法
//设置Cookie中的名称与值以及过期时间【单位:天】
Cookie.set("page", page, 100)
//获取Cookie中的值
var page = Cookie.get("page");
//移除Cookie
Cookie.remove("page");
js函数
var Cookie = {
set: function (key, value, exdays) {
//校验Key, key中不能有等号【=】
if(key.indexOf("=") !== -1) {
throw new Error("Cookie不支持key中使用等号【=】, key:" + key)
}
let exdate = new Date() // 获取时间
exdate.setTime(exdate.getTime() + 24 * 60 * 60 * 1000 * exdays) // 保存的天数
// 字符串拼接cookie
// eslint-disable-next-line camelcase
window.document.cookie = key + '=' + value + ';path=/;expires=' + exdate.toGMTString()
},
get: function (key) {
if (document.cookie.length > 0) {
// 这里显示的格式需要切割一下自己可输出看下
var arr = document.cookie.split('; ')
for (let i = 0; i < arr.length; i++) {
let arr2 = arr[i].split('=') // 再次切割
// 判断查找相对应的值
if (arr2[0] === key) {
var value = arr2[1];
for (let j = 2; j < arr2.length; j++) {
value += '=' + arr2[j];
}
return value;
}
}
}
},
remove: function (key) {
set(key, '', -1);
}
};
边栏推荐
- 不给糖就捣蛋svg万圣节js特效
- 153-Solana创建PDA和存储
- 动态规划习题(二)
- Debezium error report processing series 18: solve the problem that the table structure cannot be obtained
- The left column of WordPress implementation shows the article directory
- Small program [phase I]
- [flutter special topic] 72 graphic minimalist custom running lamp acemarquee yyds dry goods inventory
- @nonnull annotation of Lombok
- flutter jpush
- PyG教程(6):自定义消息传递网络
猜你喜欢
随机推荐
[GNN] Application of GNN neural network toolbox and MATLAB simulation
(programming exercises of various regular numbers) the prime number in the output range, the factorization prime factor of an integer, the maximum common divisor and minimum common multiple of two num
Argo CD usage
155 Solana storage array
关于#mysql#的问题,如何解决?
Pyg tutorial (5): analyzing the message propagation mechanism in GNN
[query the data in the third row of the data table]
Yield Guild Games 与 Walken 达成合作
Query the data in row n of the data table. Update table row n data
Tweenmax irregular geometry background with animation JS effect
Tweenmax oscilloscope 3D animation
Modbus poll v9.9.2 build 1690 MODBUS test tool single file version
打造硬核敲门砖——简历
Required Integer parameter ‘XXX‘ is not present
How to solve the problem of MySQL?
155-Solana存储数组
thinkphp的这些扩展插架你都知道吗?
Part 4: JVM memory model from the perspective of hardware / source code
微信小程序_6,网络数据请求
win10上vs2017配置Eigen3开发环境








![[JS] intercepting string](/img/8c/3b0f638c30e3665907dcbb9336acd8.png)