xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

cookie & maxAge & expires

cookie & maxAge & expires

Expires (timestamp) & Max-Age (seconds)

image

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie

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

https://stackoverflow.com/questions/15932957/difference-between-0-and-negative-value-for-setmaxage-for-cookie

Setting setMaxAge to 0 will delete the cookie.

Setting setMaxAge to -1 will preserve it until the browser is closed.


CORS & cookie

https://www.cnblogs.com/xgqfrms/p/9198180.html

posted @ 2018-08-09 13:24  xgqfrms  阅读(375)  评论(1编辑  收藏  举报