posted 23 years ago
hi , i am doing a project on online shopping and after generating this code i could not run the cookies . so pls let me know where the mistakes are ..
thanking in advance..
praveen..
code is below..
/* the following function returns value of a cookie */
function getCookie (name) {
var dcookie = document.cookie;
var cname = name + "=";
var clen = dcookie.length;
var cbegin = 0;
while (cbegin < clen) {
var vbegin = cbegin + cname.length;
if (dcookie.substring(cbegin, vbegin) == cname) {
var vend = dcookie.indexOf (";", vbegin);
if (vend == -1) vend = clen;
return unescape(dcookie.substring(vbegin, vend));
}
cbegin = dcookie.indexOf(" ", cbegin) + 1;
if (cbegin == 0) break;
}
return null;
}
/* the following function creates a cookie */
function setCookie (name, value, expires) {
if (!expires) expires = new Date();
document.cookie = name + "=" + escape (value) +
"; expires=" + expires.toGMTString() + "; path=/";
}
/* the following function add an item to the shopping cart */
function addItem(item)
{
var name= "Items";
var value= getCookie(name);
var twoWeeks = 2 * 7 * 24 * 60 * 60 * 1000;
var expDate= new Date();
if (value == '')
{
value = ',';
}
value = value + item + ',';
expDate.setTime(expDate.getTime() + twoWeeks);
setCookie(name, value, expDate);
}
/* the following function removes an item from the shopping cart */
function removeItem(item)
{
varname= "Items";
varvalue = getCookie(name);
var twoWeeks = 2 * 7 * 24 * 60 * 60 * 1000;
var expDate= new Date();
varsearchItem= "," + item;
varstartOfItem= value.indexOf(searchItem + ",");
varendOfItem= startOfItem + searchItem.length - 1;
if (startOfItem != -1)
{
value= value.substring(0, startOfItem) +
value.substring(endOfItem + 1, value.length);
expDate.setTime(expDate.getTime() + twoWeeks);
setCookie(name, value, expDate);
}
}
/* the following function removes all items from the shopping cart */
function removeAll()
{
var threeDays = 3 * 24 * 60 * 60 * 1000;
var expDate = new Date();
expDate.setTime(expDate.getTime() - threeDays);
setCookie('Items', ' ', expDate);
}
function thanks()
{
removeAll();
document.location.href="../term_proj/thanks.htm";
}
/* this function returns items in the cart with comma as a delimiter */
function itemsInCart()
{
return getCookie('Items');
}