Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

reg cookies and its syntax..

 
praveen gangi
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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');
}
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this JavaScript? Anyone?
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
 
Bosun Bello
Ranch Hand
Posts: 1510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, this is JavaScript. Praveen you will get better responses in the JavaScript forum. I know about cookies described in the servlet/JSP API.
------------------
Bosun
SCJP for the Java� 2 Platform
 
Manish Hatwalne
Ranch Hand
Posts: 2591
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep,
this is JavaScript and your syntax apparantly looks fine, try reading a cookie and alert its value, are you having this problem with all browsers? See if your browser is set to accept cookies, also only the domain which created the cookie can read it. I couldn't find anything wrong here (though, it's been quite some time since I have done it using JavaScript), have a look at this, and see if it is of any help, may be just a mistake in sequence, or incorrect value somewhere...
HTH,
- Manish
 
Manish Hatwalne
Ranch Hand
Posts: 2591
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK,
Here's what I have done, I used your code, and tried to add an item - "Pizza", and read it. I got - null,Pizza.
Perhaps you want to modify the "if" condition -

from function addItem(item). You haven't explained what is the problem you are facing, mind elaborating?
HTH,
- Manish
 
Preethi Suryam
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Praveen!
Could you tell us wat problem you r encountering so that we can help you.....
Regards,
Preeti
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic