当前位置:网站首页>cookie,localstorage封装
cookie,localstorage封装
2022-07-18 00:13:00 【小飞侠Pan】
cookie封装
const set = (name, value, {
maxAge, domain, path, secure} ={
}) => {
let cookieText = `${
encodeURIComponent(name)}=${
encodeURIComponent(value)}` //进行编码,防止出现乱码
if(typeof maxAge === 'number'){
cookieText += `; max-age=${
maxAge}`
}
if(domain){
cookieText += `; domain=${
domain}`
}
if(path){
cookieText += `; path=${
path}`
}
if(secure){
cookieText += `; secure`
}
document.cookie = cookieText
}
const get = (name) => {
name = `${
encodeURIComponent(name)}`
const cookies = document.cookie.split("; ")
for(const item of cookies){
const [cookieName, cookieValue] = item.split("=")
if(cookieName === name){
return decodeURIComponent(cookieValue)
}
}
return;
}
// 根据name,domain,path 删除 cookie: 将maxAge设为-1就删除了cookie
const remove = (name,{
domain,path} = {
}) => {
set(name,"",{
domain,
path,
maxAge:-1
})
}
export {
set, get, remove }
对应的测试代码
<script type="module"> import {
set, get } from './test.js'; console.log("1321546") set("username","zs") set("age",18) set("用户名","张三",{
maxAge:50 }) console.log(get("username")) </script>
localstorage封装
const Storage = window.localStorage
const set = (name,value) => {
Storage.setItem(name,JSON.stringify(value))
}
const get = (name) => {
return JSON.parse(Storage.getItem(name))
}
const remove = (name) => {
Storage.removeItem(name)
}
const clear = () => {
Storage.clear()
}
export {
set,get,remove,clear}
对应的测试代码
<script type="module"> import {
set,get,remove,clear} from './localstorage封装.js' set("user1",{
a:1, school: "456464" }) console.log(get("user1")) </script>
边栏推荐
- Work notes | talk about data quality audit
- [dynamic memory management]
- Cloud native - orchestration and management
- PostgreSQL installation et démarrage des tutoriels de base sous Linux et Windows
- C # FTP dual network card problem
- lambda函数以及对 items.sort(key = lambda y:y[1], reverse = True) 的理解。
- ssh 使用 socks5 代理连接到远端服务器中
- PbootCMS search SQL注入漏洞
- 2022-7-17
- On isp-ccm
猜你喜欢

C # FTP dual network card problem

OSPF comprehensive experiment

云原生应用的概念和云原生应用的 15 个特征

PostgreSQL in Linux and windows installation and introductory basic tutorial
Adn public welfare acceleration - jsdelivr NPM (domestic), a high-quality alternative to elmcdn

2021牛客多校训练营5(B题)

【用户文章】P4合并实践指南之实例拆解Resolve

Discussion on ISP image noise model 2

I'm new here, so please take care of me. (actually, it's not new here ^ ^, hello CSDN, I'm here.)

云原生—编排及管理
随机推荐
Is it safe to buy funds in a stock account? I want to buy funds for a long time
SSH uses Socks5 proxy to connect to the remote server
【自定义类型:结构体,枚举,联合】
Through JMeter pressure measurement surging
C语言简易版通讯录(静态版)
股票账户上买基金安全吗?我要长期买基金
Argument list too long 原因与解决思路
【软件测试】——postman接口测试工具完整教程
[advanced C language] - common memory functions
2022/7/17
Questions about Alibaba cloud's classic network
Series operations of stack and queue containers (detailed explanation)
What should I pay special attention to when opening an account? Is it safe to open a stock account by mobile phone?
恒泰证券网上开户是否安全?
股票财务信息,董事会,监事会等高管信息爬取
Selenium元素操作
OSPF综合实验
C # FTP dual network card problem
Pbootcms search SQL injection vulnerability
重庆的哪个银行网点可以买到瑞兹基金产品?