JavaScript
SHOPPING CART
A final version of the codes can be retrieved from: http://sweb.uky.edu/~dsianita/jackson3.html
1. Incorporate the shopping cart code within jackson.html
- Copy the following functions from cart.html and paste them after the placeorder() function in jackson.html: additem(), addcart(), and viewcart()
[note: we no longer allow an item be removed from the shopping cart]
- Delete the following statement from additem(): this.display=prtitem;
- Make changes (in bold) to viewcart():
function viewcart()
…
with (document)
{
write();
…
for (i in cart)
{
total = total + cart[i].price;
if (cart[i].itemcnt !=null);
{
write("<TR><TD>" + cart[i].desc +
"</TD>")
write("<TD ALIGN=RIGHT>$" + cart[i].price +
"</TD>")
write("</TR>")
}
}
write("<TR><TD>Total = </TD><TD ALIGN=RIGHT>$" + total + "</TD>");
write("</TR></TABLE><BR>");
write("</CENTER></BODY></HTML>");
…
- change the way viewcart() to be called:
<INPUT TYPE=Button VALUE="Click to view order" onClick="viewcart()">
2. A description of the intended use of the printer is stored in the shopping cart
- Make changes (in bold) to placeorder() in jackson.html:
var item=document.order.Product.options[i].text;
var itemprice=price[i];
for (var j=0; j<document.order.USE.length; j++)
{
if (document.order.USE[j].checked == true)
break;
}
item = item + " for " +
document.order.USE[j].value;
3. The overall Subtotal, Discount, and Total will be displayed when the "view order" button is clicked
- add discount property to an item stored in additem():
function additem(desc, discount, price, itemcnt)
{
this.desc = desc;
this.discount = discount;
this.price = price;
- add discount parameter to addcart():
function addcart(desc, discount, price)
{
cart[numitem] = new additem(desc, discount, price, numitem);
- add subtotal, discount, and total display in viewcart():
function viewcart()
{
var total = 0;
var totdis = 0;
var i = 0;
…
for (i in cart)
{
total = total + cart[i].price;
totdis = totdis + cart[i].discount;
…
write("<TR><TD>Subtotal = </TD><TD
ALIGN=RIGHT>$" + (total+totdis) + "</TD>");
write("<TR><TD>Discount = </TD><TD
ALIGN=RIGHT>$" + totdis + "</TD>");
write("<TR><TD>Total = </TD><TD ALIGN=RIGHT>$" + total + "</TD>");
…
- change the way addcart() is called in placeorder():
function placeorder()
…
var discount=0;
if(document.order.USE[3].checked)
{
discount=itemprice*0.05;
itemprice=itemprice *0.95;
…
addcart(item,discount,itemprice);
}