当前位置:网站首页>js工具-cookie简单封装
js工具-cookie简单封装
2022-07-27 15:55:00 【V_AYA_V】
简单封装一下cookie的一些操作方法。
/** * @desc 根据key读取cookie * @param {String} key * @return {String} cookie */
function getCookie(key) {
const arr = document.cookie.replace(/\s/g, '').split(';')
for (let i = 0; i < arr.length; i++) {
const tempArr = arr[i].split('=')
if (tempArr[0] === key) {
return decodeURIComponent(tempArr[1])
}
}
return ''
}
/** * @desc 设置Cookie * @param {String} key * @param {String} value * @param {Number} days */
function setCookie(key, value, days = 1, path = '/') {
const date = new Date()
date.setDate(date.getDate() + days)
document.cookie = `${
key}=${
value};expires=${
date};path=${
path}`
}
/** * @desc 根据key删除cookie * @param {String} key */
function removeCookie(key) {
// 设置已过期,系统会立刻删除cookie
setCookie(key, '', -1)
}
export default {
get: getCookie,
set: setCookie,
remove: removeCookie
}
边栏推荐
- From digitalization to intelligent operation and maintenance: what are the values and challenges?
- WebDriverException( selenium.common.exceptions.WebDriverException: Message: ‘chromedriver‘ executabl
- JDBC连接数据库读取前台无法显示数据
- vim的配置及基础使用
- Configuration and basic use of vim
- Convolutional neural network -- from r-cnn, fast r-cnn to fast r-cnn, mask r-cnn
- 最新大厂高级面试题 必备
- 2022 safety officer-a certificate examination questions and online simulation examination
- 【Codeforces】 A. Computer Game
- 使用分布式框架WCF出现的BUG记录
猜你喜欢

Application of knowing things and learning | correlation graph analysis in anti cheating business

kubernetes 1.24高可用集群二进制部署

Machine learning: IOU of concept understanding

js实现右键菜单栏功能

微信小程序 实现位置地图显示,引入map地图,不含导航

2022 safety officer-a certificate examination questions and online simulation examination

灵魂一问:为什么ES比MySQL更适合复杂条件搜索?

Fast parsing combined with Huatu document encryption software

机器学习——概念理解之IoU

How can we carry out NLP cross language knowledge transfer?
随机推荐
[introduction to database system (Wang Shan)] Chapter 1 - Introduction
Help, boost and take responsibility, the new value and significance of the 6th Tuba rabbit 718 national home decoration Festival
Big gap? Requirements and conditions for candidates with different academic qualifications to take the postgraduate entrance examination
备份表恢复表
Interviewer: what is scaffolding? Why do you need scaffolding? What are the commonly used scaffolds?
Mysql database defines cursor in trigger
最新大厂高级面试题 必备
灵魂一问:为什么ES比MySQL更适合复杂条件搜索?
类的六大关系——依赖和关联的区别
美团到餐“祖传数仓”标准化治理笔记
Likeshop takeout ordering system "100% open source without encryption"
6月第1周易盾业务风控关注 | 新东方学而思等15家机构被顶格罚款
x-sheet 开发教程:初始化配置自定义布局
Original direct selling MOS tube knl42150 2.8a/1500v applicable photovoltaic inverter can provide samples
防止sql注入
Yanrong technology was selected as Beijing's "specialized and innovative" in 2022 to lead hybrid cloud file storage
Behind every piece of information you collect, you can't live without TA
机器学习之评价指标(二)——分类评价指标
Flutter的布局
Telecommuting can be easily realized in only three steps