I’m using the following code for my set and get Cookie:
$.setCookie = function (c_name, value, exdays) {
var exdate = new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
document.cookie = c_name + "=" + c_value;
};
$.getCookie = function (c_name) {
var i, x, y, ARRcookies = document.cookie.split(";");
for (i = 0; i < ARRcookies.length; i++) {
x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
x = x.replace(/^\s+|\s+$/g, "");
if (x == c_name) {
return unescape(y);
}
}
};
Then I simply created two test click functions for 2 buttons:
$('#quick-true').click(function(){
$.setCookie('quick-status', true, 365);
var get = $.getCookie('quick-status');
alert('Status: '+get);
});
$('#quick-false').click(function(){
$.setCookie('quick-status', false, 365);
var get = $.getCookie('quick-status');
alert('Status: '+get)
});
But when clicking either #quick-true or #quick-false the alert says Status: undefined
Can anyone help me why this is happening. I know quite a bit about Cookies and I worked with them a lot but this time im lost.
Your code looks fine to me, but to be sure I created a test HERE, and it’s working as expected for me.
There’s probably something other than the code you have posted that is causing the problem, exactly what is hard to tell.