我使用以下代码设置Cookie,但是,当我在控制台中使用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 :(得分:0)
您要保存的Cookie应为对象
var sProperties = {};