Javascript 设置cookies不需要';I don’我没有按预期工作

Javascript 设置cookies不需要';I don’我没有按预期工作,javascript,php,wordpress,cookies,Javascript,Php,Wordpress,Cookies,我正在努力实现的目标: 按下href时,我调用saveItem()函数。其名称如下: 就像你自己做的那样是cookie中介于值、过期日期等之间的字段分隔符。您不能使用它来分隔值。好的,所以问题出在分隔符和我的代码中-我检查ID是否已存在的方法是错误的,但下面的方法可行 saveItem() 函数保存项(名称、值、天数){ var=”; 如果(天){ 变量日期=新日期(); date.setTime(date.getTime()+(天*24*60*60*1000)); expires=“;expi

我正在努力实现的目标:

按下href时,我调用
saveItem()
函数。其名称如下:


就像你自己做的那样
是cookie中介于值、过期日期等之间的字段分隔符。您不能使用它来分隔值。

好的,所以问题出在分隔符和我的代码中-我检查ID是否已存在的方法是错误的,但下面的方法可行

saveItem()

函数保存项(名称、值、天数){
var=”;
如果(天){
变量日期=新日期();
date.setTime(date.getTime()+(天*24*60*60*1000));
expires=“;expires=“+date.toutString();
}
var mNameList=getCookie(名称);
如果(mNameList==“”){
document.cookie=name+“=”+value+expires+“path=/”;
}否则{
var doesItemExists=false;
var partsOfStr=mNameList.split('-');
对于(变量i=0;i”+mNameList+“|currentID->”+value);
}
}
}
print_r($_COOKIE['savedList']);
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);
        }
    }
}
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 "";
    }
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);
            }
        }
    }