cookie & maxAge & expires
cookie & maxAge & expires
Expires (timestamp
) & Max-Age (seconds )
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie
cookie demo
create & delete
https://codepen.io/webgeeker/full/vaQRqe/
setTimeout(() => {
let btn = document.querySelector(`[data-link="logout"]`);
btn.addEventListener(`click`, function (e) {
// clear cookie
document.cookie = "access_token=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
setTimeout(() => {
window.parent.location.replace(`http://10.1.5.202/auto-deploy-platform/login/index.html`);
}, 0);
});
}, 1000);
// obj = {key: "", value: "", timestamp: 36*Math.pow(10, 6)};// one day & ms
// {key: "", value: "", timestamp: 36000000}
/*
new Date(new Date().getTime() + 36000*1000).toUTCString();
"Thu, 09 Aug 2018 13:44:09 GMT"
new Date().toUTCString();
"Thu, 09 Aug 2018 03:44:15 GMT"
*/
let ga = {
key: "_ga",
value: "GA1.2.156580446.1505450776",
timestamp: 36*Math.pow(10, 6)
};
// one day & ms
const setCookie = (obj = {key: "", value: "", timestamp: 36000*1000}, debug = false) => {
let {
key,
value,
timestamp
} = obj;
let host = window.parent.location.origin;
if(key && value) {
// let expires = new Date().toUTCString();
let expires = new Date(new Date().getTime() + 36000*1000).toUTCString();
let domain = host.includes(`https`) ? host.slice(8) : host.slice(7);
document.cookie = `${key}=${value}; expires=${expires}; path=/; domain=${domain};`;
}
};
setCookie(ga);
const deleteCookie = (key = ``, debug = false) => {
let host = window.parent.location.origin;
if(key) {
let expires = new Date("1969-01-01").toUTCString();
let domain = host.includes(`https`) ? host.slice(8) : host.slice(7);
document.cookie = `${key}=; expires=${expires}; path=/; domain=${domain};`;
}
};
deleteCookie(ga.key);
setMaxAge
Setting
setMaxAge
to0
will delete the cookie.
Setting
setMaxAge
to-1
will preserve it until the browser is closed.
CORS & cookie
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/9448341.html
未经授权禁止转载,违者必究!