当前位置:网站首页>JS operation cookie, JS setting cookie value, JS reading cookie value
JS operation cookie, JS setting cookie value, JS reading cookie value
2022-06-21 07:05:00 【Just a craftsman】
js Yes Cookie The operation of
Preface :
Here is a brief introduction to the use of java Yes Cookie How to operate , But it is not recommended to use Cookie, Because some users will disable the website Cookie, If used improperly, users will not be able to use the website , So for Cookie Give a brief introduction , Understanding is good. .
js Function usage
// Set up Cookie Name and value in and expiration time 【 Company : God 】
Cookie.set("page", page, 100)
// obtain Cookie The value in
var page = Cookie.get("page");
// remove Cookie
Cookie.remove("page");
js function
var Cookie = {
set: function (key, value, exdays) {
// check Key, key Cannot have an equal sign in 【=】
if(key.indexOf("=") !== -1) {
throw new Error("Cookie I won't support it key Use the equal sign in 【=】, key:" + key)
}
let exdate = new Date() // Acquisition time
exdate.setTime(exdate.getTime() + 24 * 60 * 60 * 1000 * exdays) // Days saved
// String splicing cookie
// eslint-disable-next-line camelcase
window.document.cookie = key + '=' + value + ';path=/;expires=' + exdate.toGMTString()
},
get: function (key) {
if (document.cookie.length > 0) {
// The format shown here needs to be cut. You can output it and have a look
var arr = document.cookie.split('; ')
for (let i = 0; i < arr.length; i++) {
let arr2 = arr[i].split('=') // Cut again
// Judge and find the corresponding value
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);
}
};
边栏推荐
- Eigen common operations
- On June 13, 2022, interview questions were asked
- Feature scaling for machine learning
- Debezium error report processing series 18: solve the problem that the table structure cannot be obtained
- Bol Bohr's original dual currency driving model leads the new hotspot of dfi+nft+web3.0
- 工作那点事
- IDM移动端功能升级说明
- 153 Solana create PDA and storage
- Geo2r: difference analysis of data in geo database
- Argo CD 使用
猜你喜欢
The origin of Butler Volmer formula

The database has the problem of user changing password

BOL波尔独创双币驱动模型 引领DeFi+NFT+Web3.0新热点

Microphone loading animation

C language program design - Sanzi chess (semester homework)

Bloom filter

How to deal with the error message of concurrentmodificationexception?

Wechat applet_ 5. Global configuration

Configuring the eigen3 development environment for vs2017 on win10

天气预报小程序源码/天气类微信小程序源码
随机推荐
I2C驱动实现的两种思路(i2c-dev.c和i2c-core.c)
Hub, switch, router
Feature scaling for machine learning
flutter jpush
使用Loupe Cell Browser查看10X单细胞转录组分析结果
[middle order traversal of binary tree based on stack] middle order traversal of binary tree + stack, spatial complexity of O (H)
ArcEngine secondary development based on C 57: the owner Sid on each user subscription does not exist
【GNN】GNN图神经网络工具箱的应用和matlab仿真
【osg】OSG开发(03)——构建MSVC版的osgQt库
The origin of Butler Volmer formula
Two column set (map set)
Wechat applet_ 6. Network data request
微信小程序_3,WXML模板语法
Markdown mathematical grammar [detailed summary]
Analyse des données: indicateurs communs pour différentes industries
WordPress实现左边栏显示文章目录
动态规划习题(二)
Tweenmax irregular geometry background with animation JS effect
Hamming code verification [simple and detailed]
Modbus poll v9.9.2 build 1690 MODBUS test tool single file version