IE Edge全局cookie未在首次操作时设置

时间:2018-08-31 09:37:13

标签: javascript internet-explorer cookies

具有以下代码,可在浏览器中设置全局Cookie

function detectIE() {
    var ua = window.navigator.userAgent;

    var msie = ua.indexOf('MSIE ');
    if (msie > 0) {
        // IE 10 or older => return version number
        return parseInt(ua.substring(msie + 5, ua.indexOf('.', msie)), 10);
    }

    var trident = ua.indexOf('Trident/');
    if (trident > 0) {
        // IE 11 => return version number
        var rv = ua.indexOf('rv:');
        return parseInt(ua.substring(rv + 3, ua.indexOf('.', rv)), 10);
    }

    var edge = ua.indexOf('Edge/');
    if (edge > 0) {
        // Edge (IE 12+) => return version number
        return parseInt(ua.substring(edge + 5, ua.indexOf('.', edge)), 10);
    }

    // other browser
    return false;
}

function createCookie(name, value, days) {
    var expires = "";
    var ieversion = detectIE();
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
//      expires = "; expires=" + date.toUTCString();
        if(ieversion == false || ieversion > 11) {

            expires = "; expires=''";
        }
    }
    document.cookie = name + "=" + value + expires + "; path=/";
}

createCookie('cookiePolicyAccepted', 'true', 7)

将首先无法在Internet Explorer Edge中执行。如果我导航到另一页,则document.cookie为空,如果再次设置,则一切正常。

如何调试此问题?

0 个答案:

没有答案