forgot the script(re cookies)

admin

Administrator
Staff member
cookies help please
can any body help with the following problem. i have this script which hold items from the web page in an array. not what i want to do is every time a item is added to the basket i want to write to a cookie then when a new page is loaded i want to be able to read the cookie back to the user. then if the user select another product the content of the cookie are updated.
Please help asd this is a uni assignment and due in soon, many thANKS
Andrew lAWS


__________________
andy


var cart = new ShoppingCart();



function Product(name, num, price, shipping, qty) {

this.name = name;

this.catalogNumber = num;

this.price = price;

this.shippingCost = shipping;

this.qty = qty;

this.totalCost = totalCost;
}



function totalCost() {

return ((this.price + this.shippingCost) * this.qty);
}



function ShoppingCart() {

this.items = new Array();

this.addItem = addItem;

this.removeItem = removeItem;


}


function addItem(item) {

var i;

for (i = 0; i < this.items.length; i++)

if (this.items.catalogNumber == item.catalogNumber) {

this.items.qty++;

return;
}


this.items[this.items.length] = item;

}



function removeItem(catno) {

var i, j;
var tmp;



for (i = 0; i < this.items.length; i++)
if (this.items.catalogNumber == catno) {
this.items.qty--;
if (this.items.qty > 0)
return;



tmp = new Array();
for (j = 0; j < i; j++)
tmp[tmp.length] = this.items[j];
for (j = i + 1; j < this.items.length; j++)
tmp[tmp.length] = this.items[j];
this.items = null;
this.items = tmp;
}
}



function displayCart() {

var i;

var s;

var total;

var newWin = window.open('', 'myNewWin', 'width=770,height=777,toolbar=1,menubar=1,resizable=0');
newWin.document.write("<h3>Your baskett contains</h3>");
newWin.document.open("text/html", "replace");
newWin.document.writeln('<html><head><body>');
newWin.document.writeln('<font face="Arial,Helvetica" size=3>');
newWin.document.writeln('<b>Shopping Cart:</b>');
newWin.document.writeln('<p>');
newWin.document.writeln('<table border=1 cellpadding=2 cellspacing=0>');

s = '<th>Name</th>' +
'<th>Catalog No</th>' +
'<th>Price</th>' +
'<th>Shipping Cost</th>' +
'<th>qty</font></th>' +
'<th>Total Cost</font></th>';

newWin.document.writeln(s);

total = 0;
for (i = 0; i < cart.items.length; i++) {
newWin.document.writeln('<tr>');
s = '<td>' + cart.items.name+ '</td>' +
'<td>' + cart.items.catalogNumber + '</td>' +
'<td>' + format(cart.items.price) + '</td>' +
'<td>' + format(cart.items.shippingCost) + '</td>' +
'<td>' + cart.items.qty + '</td>' +
'<td>' + format(cart.items.totalCost()) + '</td>';
newWin.document.writeln(s);
total += cart.items.totalCost();
}

newWin.document.writeln('</table>');
newWin.document.writeln('<p>');
newWin.document.writeln('<b>Total Order:</b> ' + format(total));
newWin.document.writeln('</font>');
newWin.document.writeln('</body></html>');
newWin.document.close();
}



function format(n) {

var s = ":";
var d, c;

d = parseInt(n, 10);
c = Math.round((n - d) * 100);
if (c == 0)
c = "00";
else if (c < 9)
c = "0" + c;
return(":" + d + "." + c);
}
 
Back
Top