Wednesday, July 8, 2009

Business Catalyst - Create, Read and Erase Cookie Javascript


function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
var nameEQ = 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;
}

function eraseCookie(name) {
createCookie(name,"",-1);
}

createCookie('testcookie','valcookie',7);
var cookie_value = readCookie('testcookie');
alert('cookie value = '+cookie_value); //output cookie value valcookie
eraseCookie('menard');
cookie_value = readCookie('menard');
alert('cookie value = '+cookie_value); //cookie value menard null

2 comments:

  1. Thanks for posting and the tips you shared to us about business catalyst.

    ReplyDelete
  2. Do this support Embedding Social Sites like Facebook and Twitter Update?
    I found this one Business Catalyst but still want to know more deeply in this CMS because I find it more interesting when exchaging thougths from fellow developers rather than cooked posts.
    Thank you for your post!

    ReplyDelete