2014-07-21 4 views
0

다음 코드를 사용하여 쿠키를 설정하고 있지만 콘솔에서 document.cookie을 사용하면 setItem()을 통해 전달 된 키 - 값 쌍이 인쇄되지 않습니다. 아무도 그 문제가 뭔지 알아?쿠키가 항상 설정되지 않습니다

var docCookies = { 
    getItem: function (sKey) { 
    return decodeURIComponent(document.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*" + encodeURIComponent(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")) || null; 
    }, 
    setItem: function (sProperties) { 

    var cookieString = ""; 
    var sExpires = ""; 

    for (var sKey in sProperties){ 
     console.log("inside for"); 
     if (sProperties.hasOwnProperty(sKey)) { 
      // alert("Key is " + k + ", value is" + target[k]); 
      console.log("inside if"); 
      cookieString += encodeURIComponent(sKey) + "=" + encodeURIComponent(sProperties[sKey])+"; "; 


      } 
    } 
    console.log("outside for"); 

    document.cookie = cookieString.substring(0,cookieString.length-2); 
    return true; 
    }, 
    hasItem: function (sKey) { 
    return (new RegExp("(?:^|;\\s*)" + encodeURIComponent(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=")).test(document.cookie); 
    } 

}; 



var expireDate = (new Date((new Date()).getTime() + (60*24*60*60*1000))).toUTCString(); 
var sProperties = []; 
sProperties['_country'] = _country ; 
sProperties['_language'] = _language ; 
sProperties['_pageName'] = _pageName ; 
sProperties['_products'] = _products ; 
sProperties['_events'] = _events ; 
sProperties['_server'] = _server ; 
sProperties['_href'] = _href ; 
sProperties['expires'] = expireDate ; 

docCookies.setItem(sProperties); 

답변

0

당신은 내가 모든 배열 값을 읽을

var sProperties = {}; 
+0

객체에게하고 당신이 자세히 보면 – Blake

+0

그래 내가 알고있는'cookieString'에 넣어해야 저장할 쿠키, 나는이'VAR을 의미 s 속성 = []; sProperties [ '_ country'] = _ 국가;가 잘못되었습니다. s 속성에는 값이 없습니다. 내가 뭐 놓친 거 없니? –

관련 문제