I am creating a cookie when a user clicks on a link and saving a value based on the link clicked, everytime the user goes to a different page i want the JS to check if the cookie already exists and use the value stored in it or if not to then create the cookie and save the selected value. The cookie can also change value based on the different links that the user clicks. Please find below what i have so far:
Code:
function font_size(size)
{
var checkCookie = getCookie('testcookie');
if (checkCookie == null)
{
if(size=='small') {
//document.body.style.fontSize = '10';
setCookie('10');
}
else if (size=='normal')
document.body.style.fontSize = '12';
else
document.body.style.fontSize = '14';
}
}
function setCookie(size)
{
var theDate = new Date();
var oneYearLater = new Date( theDate.getTime() + 31536000000 );
var expiryDate = oneYearLater.toGMTString();
document.cookie = 'testcookie=test;expires='+expiryDate+';path=/';
}
function getCookie(cookie_name)
{
var nameEQ = cookie_name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
Comment