设置cookie并不像预期的那样工作

时间:2017-02-23 08:35:18

标签: javascript php wordpress cookies

我想要实现的目标:

当按下href时,我调用saveItem()功能。它的名称如下:

<a href="#" class="save-product" onclick="saveItem('savedList', '<?php echo get_the_ID();?>', 90)">

savedList =我的Cookie名称

get_the_ID() =一个wordpress功能,用于获取当前的ID帖子,&#39; 185&#39;等

90 = Cookie过期的日期

当调用saveItem()时,它会检查名称为savedList的cookie是否已存在。如果它没有,它会创建一个具有该名称的cookie,并添加通过参数传递的值(当前帖子的id)。 当这个cookie存在时,我想再向该cookie添加一个id,分隔符将是;所以我可以 - 在另一个页面中显示通过该cookie列表的产品列表。

所以我的cookie有&#34; 185&#34; 。当我添加新ID时,例如&#34; 65&#34;,我希望我的cookie成为&#34; 185; 65&#34;

我的问题它没有按预期工作。奇怪的是,如果我在console.log("New Value Is : " + newValue);上看到它显示&#34; 185; 65&#34;但 console.log(mNameList);仅显示&#34; 185&#34;试。

要查看我使用的Cookie值:

print_r($_COOKIE['savedList']);

以下功能:

saveItem()

function saveItem(name,value,days) {

    var expires = "";
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days*24*60*60*1000));
        expires = "; expires=" + date.toUTCString();
    }

    // Get Cookie
    var mNameList = getCookie(name);

    // If cookie is empty - doesn't exist then create it and put the value given
    if (mNameList == "") {
        document.cookie = name + "=" + value + expires + "; path=/";
    } else {

        // If cookie exists, check if it has already this value, if it doesn't then add oldvalue + new value to that cookie.
        if(mNameList !== value){
            var newValue = mNameList + ';' + value; // "185;65"
            document.cookie = name + "=" + newValue + expires + "; path=/"; 
            console.log("New Value Is : " + newValue);
            var mNameList = getCookie(name); // Το check current cookie get it again
            console.log(mNameList); // Show it - here it shows "185"
        }
        else{
            // Value already exists in cookie - don't add it
            console.log("Are same - mNameList->" + mNameList + "  |   currentID->" + value);
        }
    }
}

的getCookie();

function getCookie(cname) {
        var name = cname + "=";
        var decodedCookie = decodeURIComponent(document.cookie);
        var ca = decodedCookie.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 "";
    }

2 个答案:

答案 0 :(得分:1)

正如您自己做的那样;是Cookie中的字段分隔符,值,到期等。您不能使用它来分隔您的值。

答案 1 :(得分:0)

好的问题是在分隔符和我的代码中 - 我检查ID是否已经存在的方式是错误的,但下面的工作正常。

saveItem()

function saveItem(name,value,days) {

        var expires = "";
        if (days) {
            var date = new Date();
            date.setTime(date.getTime() + (days*24*60*60*1000));
            expires = "; expires=" + date.toUTCString();
        }

        var mNameList = getCookie(name);

        if (mNameList == "") {
            document.cookie = name + "=" + value + expires + "; path=/";
        } else {

            var doesItemExists = false;
            var partsOfStr = mNameList.split('-');

            for(var i =0; i < partsOfStr.length; i++){
                if(partsOfStr[i] == value)
                    doesItemExists = true;
            }

            if(!doesItemExists){
                var newValue = mNameList + '-' + value;
                document.cookie = name + "=" + newValue + expires + "; path=/";
                console.log("New Value Is : " + newValue);
            }
            else{
                console.log("Are same - mNameList->" + mNameList + "  |   currentID->" + value);
            }
        }
    }