/    Sign up×
Community /Pin to ProfileBookmark

forgot the script(re cookies)

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[i].catalogNumber == item.catalogNumber) {

this.items[i].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[i].catalogNumber == catno) {
this.items[i].qty–;
if (this.items[i].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[i].name+ ‘</td>’ +
‘<td>’ + cart.items[i].catalogNumber + ‘</td>’ +
‘<td>’ + format(cart.items[i].price) + ‘</td>’ +
‘<td>’ + format(cart.items[i].shippingCost) + ‘</td>’ +
‘<td>’ + cart.items[i].qty + ‘</td>’ +
‘<td>’ + format(cart.items[i].totalCost()) + ‘</td>’;
newWin.document.writeln(s);
total += cart.items[i].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);
}

to post a comment
JavaScript

0Be the first to comment 😎

×

Success!

Help @andy_117 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...