aspose file tools*
The moose likes Java in General and the fly likes reg cookies and its syntax.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "reg cookies and its syntax.." Watch "reg cookies and its syntax.." New topic
Author

reg cookies and its syntax..

praveen gangi
Greenhorn

Joined: Dec 06, 2001
Posts: 1
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

Joined: Oct 25, 2000
Posts: 7292

Is this JavaScript? Anyone?
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide


Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
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


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2578

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

Joined: Sep 22, 2001
Posts: 2578

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

Joined: Nov 17, 2000
Posts: 92
Hi Praveen!
Could you tell us wat problem you r encountering so that we can help you.....
Regards,
Preeti
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: reg cookies and its syntax..