Java Reference
In-Depth Information
detailsWindow.close();
}
The fi nal function displays the contents of the shopping basket in a new window.
function showBasket()
{
basketWindow = window.open('about:blank','shoppingBasket',
'width=400,height=350');
basketWindow.document.open();
var basketItem;
var containsItems = false;
basketWindow.document.write(“<h4>Your shopping basket contains :</h4>”);
for (var i = 0; i < stockItems.length; i++)
{
var stockItem = stockItems[i];
if (stockItem.quantity > 0)
{
basketWindow.document.write(stockItem.title + “ at “);
basketWindow.document.write(stockItem.price);
basketWindow.document.write(“    ”);
basketWindow.document.write(“<a href='' onclick='return “
+ “window.opener.removeItem(“ + i + “)'>”);
basketWindow.document.write(“Remove Item</a><br />”);
containsItems = true;
}
}
if (!containsItems)
{
basketWindow.document.write(“<h4>No items</h4>”);
}
basketWindow.document.close();
basketWindow.focus();
}
First, you open a new window and store its
window
object reference in
basketWindow
. You then write
a heading to the new window's
document
, and then you loop through each item in the
stockItems
array and check the
quantity
property of the
StockItem
object. If it is greater than zero, you write the
book's details to the shopping list window. You also write out a link to the shopping basket that when
clicked calls your
removeItem()
function.
Finally, you need to create the topic description pages. First you have
pro_ajax_details.htm
. This
is identical to the version you created for the example, except for the addition of the form and button
inside. When clicked, the button calls the
addToBasket()
function in the window that opened this
window — that is,
ch8_q2_online_books.htm
. The highlighted portion of the following code shows
the changes made to this fi le:
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>