当前位置:网站首页>Cookies, localstorage encapsulation
Cookies, localstorage encapsulation
2022-07-19 15:55:00 【Pan xiaofeixia】
cookie encapsulation
const set = (name, value, {
maxAge, domain, path, secure} ={
}) => {
let cookieText = `${
encodeURIComponent(name)}=${
encodeURIComponent(value)}` // Encoding , To prevent the occurrence of random code
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;
}
// according to name,domain,path Delete cookie: take maxAge Set to -1 Is deleted cookie
const remove = (name,{
domain,path} = {
}) => {
set(name,"",{
domain,
path,
maxAge:-1
})
}
export {
set, get, remove }
The corresponding test code
<script type="module"> import {
set, get } from './test.js'; console.log("1321546") set("username","zs") set("age",18) set(" user name "," Zhang San ",{
maxAge:50 }) console.log(get("username")) </script>
localstorage encapsulation
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}
The corresponding test code
<script type="module"> import {
set,get,remove,clear} from './localstorage encapsulation .js' set("user1",{
a:1, school: "456464" }) console.log(get("user1")) </script>
边栏推荐
- 洗澡的新发现
- Adn public welfare acceleration - jsdelivr NPM (domestic), a high-quality alternative to elmcdn
- Identity Server 4使用OpenID Connect添加用户身份验证(三)
- 一文彻底理解BIO、NIO、AIO
- LBP feature notes
- Discussion on ISP noise model 1
- Online sql to text tool
- Which bank outlet in Chongqing can buy Ritz fund products?
- Reflector uses detailed explanation to convert DLL files into CS file
- 2022-7-17
猜你喜欢

如何正确重写hashCode方法?

Discussion on ISP image noise model 2

阿叶的新发型

MogDB/openGauss 权限整理

Woman Yelling At a Cat

鲲鹏代码迁移工具基础知识

BFD of HCNP Routing & Switching

使用ZVS驱动无线充电线圈

C # minimize the WinForm software to the system tray, and start up automatically

Case study on data management of low code building design company
随机推荐
Reflector使用详解,把DLL文件转换为.cs文件
ORACLE中行锁问题排查手段
Reflector uses detailed explanation to convert DLL files into CS file
我应该怎么设计我的博客?如何搭建一个体验好的博客?
进阶C语言 - 结构体实现位段
Is Guotai Junan a regular securities company? Is it safe to open an account?
进 /user/用户名/Library/Application Support/
Build intranet mail service through Qunhui Suite
Argument list too long causes and Solutions
Which bank outlet in Chongqing can buy Ritz fund products?
群里的初级工程师求助说,要采集采招数据,必须给他安排上
Rap phenomenon in wisdom tooth extraction
The lifecycle of arkui development framework components
The junior engineer in the group asked for help and said that if he wanted to collect the recruitment data, he must be arranged
投资的意义
智牛股--08
C# 实现winform软件最小化到系统托盘,开机自启动
Wpa_ Supplicant WiFi connection
排序子序列与倒置字符串
The use of "!" in vscode is invalid, and there is no solution to the template problem