Set-Cookie: Expire property, clock skew and Internet Explorer issue

前端 未结 4 1528
北恋
北恋 2020-12-05 21:42

There is a header Max-Age that allows to specify the expiration time of a cookie. Unfortunately Internet Explorer 6, 7, 8 and probably later do not support Max-Age and requi

4条回答
  •  有刺的猬
    2020-12-05 22:01

    • Set Max-Age as everyone but Microsoft understands it.
    • Add Javascript that runs only on IE to convert Max-Age to UTC according to the browser's clock and set that expiration time on the cookie. Note that JavaScript cannot read the Max-Age set in the cookie, so you will have to provide that information (along with any other options) to the JavaScript some other way.

    From QuirksMode

    function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
    }
    

    Then after you get the cookie name and maxAge and otherOptions (e.g. path, domain) from somewhere:

    var date = new Date();
    date.setTime(date.getTime() + (maxAge * 1000));
    document.cookie = name + "=" + readCookie(name) + 
        '; expires=' + date.toUTCString() + otherOptions
    

提交回复
热议问题