JavaScript split方法仅在Safari中返回空数组

时间:2019-05-17 16:20:58

标签: javascript string cookies split safari

我有一个cookie,必须在其中除以(“;”)。在chrome(浏览器)中,IE向我返回了所有拆分部分的数组,但在野生动物园中,它从完整字符串中返回了一个空数组。

Cookie字符串:

"pre-agendamento=2261$Horm; mobile=-1; WSS_FullScreenMode=false; _ga=GA1.3.1074191601.1558110091; _gid=GA1.3.732210393.1558110091; WSS_KeepSessionAuthenticated={f0eeda9a-6e01-4bf4-8dd4-0b832aa835cd}; WeinmannMedico=true; _gcl_au=1.1.1992728444.1558110091"

其他字符串方法,例如.replace()和其他方法都可以正常工作。

function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays*24*60*60*1000));
    var expires = "expires="+ d.toUTCString();
    document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
};

function getCookie(cname) {
    var name = cname + "=";
    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);
        }
        if (c.indexOf(name) == 0) {
            return c.substring(name.length,c.length);
        }
    }
    return "";
};

没有显示错误。

预期:

VAR ca == ["mobile=-1", " WSS_FullScreenMode=false", " _ga=GA1.3.1074191601.1558110091", " _gid=GA1.3.732210393.1558110091", " WSS_KeepSessionAuthenticated={f0eeda9a-6e01-4bf4-8dd4-0b832aa835cd}", " WeinmannMedico=true", " _gcl_au=1.1.1992728444.1558110091"]

实际:

VAR ca == []

0 个答案:

没有答案