2017-02-23 62 views
0

我想实现的是:设置cookie不能正常工作

当按下href时,我呼叫saveItem()函数。它被称为如下:

​​

savedList =我的饼干

get_the_ID() = A WordPress的函数来获取当前的ID后的名字, '185' 等

=天的cookie到期

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

所以我的cookie有“185”。当我添加一个新的ID,例如“65”,我希望我的cookie变成“185; 65”

我的问题是,它不能按预期方式工作。奇怪的是,如果我看到console.log("New Value Is : " + newValue);显示“185; 65”,但 console.log(mNameList);只显示“185”。

要检查我的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 ""; 
    } 

回答

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); 
      } 
     } 
    } 
1

当你自己做;在到期值之间的饼干,等你不能用它来单独您的值的字段分隔符。