2017-02-23 130 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

當你自己做;在到期值之間的餅乾,等你不能用它來單獨您的值的字段分隔符。