Friday, 27 September 2013

Cookie not geting saved on page reload

Cookie not geting saved on page reload

I have this code
$(document).ready(function () {
var d = new Date();
var newMinutes = d.getTimezoneOffset();
var storedMinutes = getCookieValue("tzom");
if (newMinutes != storedMinutes) {
setCookie("tzom", newMinutes);
alert("new" + newMinutes);
alert("stored" + storedMinutes);
document.location.reload(true);
}
});
What I'm trying to do here is check a timezone value in a cookie to see if
it is different to the current timezone set by the user. This will happen
if I user comes to the site and changes their timezone and then goes to
another page. If there is a difference then I set the cookie with the new
value and reload the page. The problem is that when I reload the page it
still shows that there is a difference in the timezones and so the page
reloads in an infinite loop. If I am somehow able to click on a different
page during this infinite loop then the looping stops.
What is wrong here?
EDIT
Rest of the code is here
function getCookieValue(c_name) {
var c_value = document.cookie;
var c_start = c_value.indexOf(" " + c_name + "=");
if (c_start == -1) {
c_start = c_value.indexOf(c_name + "=");
}
if (c_start == -1) {
c_value = null;
}
else {
c_start = c_value.indexOf("=", c_start) + 1;
var c_end = c_value.indexOf(";", c_start);
if (c_end == -1) {
c_end = c_value.length;
}
c_value = unescape(c_value.substring(c_start, c_end));
}
return c_value;
}
function setCookie(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;
}

No comments:

Post a Comment