Jump to content

Lynda.com Help


madchillidog
 Share

Recommended Posts

Hi Everyone,Since I'm using the tutorials at Lynda.com which, unfortunately, has no forum to discuss the lessons- I'm coming here.I am not having any luck getting past an error message "initialize is not defined". I'm using a Mac, so I guess using IE5 has some issues with xml data islands, so that alone may contribute to my problem. I use Firefox and Opera to view my results. I'm debugging my script that I hand coded along side the lesson, but I cannot find where I'm going wrong. I have a hunch it's in the DOCTYPE tag, or an error in the js code that stops the page from working correctly. Here is the html page and .js page, respectively:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head>	<title>Bringing It All Together</title><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/><script language="javascript" type="text/javascript" src="xmlcafescript.js"></script>	</head><body bgcolor="#FFFFFF" text="#000000"	onload="initialize('menuTable','cafeMenuXML');	document.forms[0].txtBillAmt.value=calculateBill('menuTable');">	<xml id="cafeMenuXml">	<cafemenu>		<section name="Side Dishes">			<entree vegetarian="true">				<item>Potato Salad</item>				<price>2.95</price>			</entree>			<entree>				<item>My French Fries</item>				<price>5.50</price>			</entree>			<entree>				<item>Macho Nachos</item>			</entree>		</section>		<section name="Breakfast">			<entree>				<item>Two Eggs, any style</item>				<price>2.99</price>			</entree>			<entree>				<item>My Famous Pancakes</item>				<price>6.50</price>			</entree>			<entree vegetarian="true">				<item>Fruit Cocktail</item>				<price>4.99</price>			</entree>			<entree vegetarian="true">				<item>French Toast</item>				<price>3.59</price>			</entree>		</section>		<section name="Lunch">			<entree>				<item>Grilled Chicken Sandwich</item>				<price>5.99</price>			</entree>			<entree>				<item>Tuna Melt</item>				<price>4.99</price>			</entree>			<entree vegetarian="true">				<item>Pasta Salad</item>				<price>3.99</price>			</entree>		</section>		<section name="Dinner">			<entree>				<item>New York Steak</item>				<price>9.99</price>			</entree>			<entree>				<item>Thai Red Snapper</item>				<price>14.99</price>			</entree>			<entree>				<item>Linguini with Grilled Porcini Mushrooms</item>				<price>8.99</price>			</entree>			</section>		</cafemenu></xml><h2>Welcome To My Cafe</h2><p>Select your entrees from the menu below. To calculate the amount of the bill, click the Calculate Bill button. Check the "Highlight Vegetarian Meals" box to highlight vegetarian dishes.</p> <table id="menuTable" border="1" class="indent">	<thead>		<tr>			<th colspan="3">My Cafe Menu</th>		</tr>		<tr>			<th> </th>			<th><strong>Item</strong></th>			<th>Price</th>		</tr>	</thead>	<tbody>	</tbody></table><form class="indent">	<p>		<input type="button" name="btnCalcBill" value="Calculate Bill"				onClick="document.forms[0].txtBillAmt.value=calculateBill('menuTable');"/>		Total: $		<input type="text" name="txtBillAmt"/>		<input type="checkbox" name="cbOpts" value="isVeg"				onClick="highlightVegetarian('menuTable', this.checked)"/>		Highlight Vegetarian Meals </p></form></body></html>

*********************************

var gEntreeCount = 0;function initialize(idTheTable,idXMLData){	var oTheTable = document.getElementById(idTheTable);		var theTBODY = oTheTable.getElementsByTagName('TBODY');	if (theTBODY.length > 0)		oTheTable.removeChild(theTBODY[0]);			theTBODY = generateMenuContent(idXMLData);		oTheTable.appendChild(theTBODY);}	//build eveything here for the TBODY	function generateMenuContent (idXMLData){			var i=0, j=0;				var theTBODYNode = document.createElement('TBODY');				var oXMLDoc = document.getElementById(idXMLData);				var aMenuSections = oXMLDoc.getElementsByTagName('section');		for (i=0; i < aMenuSections.length; i++) {						var sName = aMenuSections.item(i).getAttribute('name');			var oTR = document.createElement('TR');			var oTD = document.createElement('TD');			oTD.setAttribute('colspan', '3');			oTD.appendChild(document.createTextNode(sName));			oTR.appendChild(oTD);			theTBODYNode.appendChild(oTR);						var aEntrees = aMenuSections.item(i).getElementsByTagName('entree');			for (j=0; j < aEntrees.length; j++){								oTR = document.createElement('TR');				if (aEntrees.item(j).getAttribute("vegetarian"))					oTR.setAttribute("vegetarian", aEntrees.item(j).getAttribute("vegetarian"));									oTD = document.createElement('TD');				oTD.setAttribute('align', 'center');				var oCB = document.createElement ('INPUT');				oCB.setAttribute('name', 'item' + gEntreeCount++);				oCB.setAttribute('type', 'checkbox');				oTD.appendChild(oCB);				oTR.appendChild(oTD);								oTD = document.createElement('TD');				var oItemNode = aEntrees.item(j).getElementsByTagName('item')[0];				oTD.appendChild(document.createTextNode(oItemNode.firstChild.data));				oTR.appendChild(oTD);								oTD = document.createElement('TD');				oTD.setAttribute('align','right');				var oPriceNode = aEntrees.item(j).getElementsByTagName('price')[0];				oTD.appendChild(document.createTextNode(oPriceNode.firstChild.data));				oTR.appendChild(oTD);								theTBODYNode.appendChild(oTR);							}					}		return theTBODYNode;	}		function calculateBill(idMenuTable){			var fBillTotal = 0.0;			var i=0;						var oTable = document.getElementById(idMenuTable);			var aCBTags = oTable.getElementsByTagName('INPUT');			for (i=0; i < aCBTags.length; i++)				if (aCBTags[i].checked){										var oTR = getParentTag(aCBTags[i], 'TR');					var oTDPrice = oTR.getElementsByTagName('TD')[2];					fBillTotal += parseFloat(oTDPrice.firstChild.data);				}		}		return Math.round(fBillTotal*100.0)/100.0;	}			function highlightVegetarian(idTable,bShowVeg){						var i=0;			var oTable = document.getElementById(idTable);						var oTBODY = oTable.getElementsByTagName('TBODY')[0];			var aTRs = oTBODY.getElementsBYTagName('TR');			for (i=0; i < aTRs.length; i++){								if (aTRs[i].getAttribute('vegetarian')) {					if (bShowVeg)						aTRs[i].style.backgroundColor = "lightGreen";					else						aTRs[i].style.backgroundColor = "";										}			}		}				function getParentTag(oNode, sParentType){						var oParent = oNode.parentNode;			while (oParent){								if (oParent.nodeName == sParentType)					return oParent;				oParent = oParent.parentNode;			}			return oParent;	}

Thanks to anyone who can take a look!

Link to comment
Share on other sites

1. Add a price node for Macho Nachos. You did not account for missing price nodes2. Correct the Calculate Bill function . It has the return outside of the functionWRONG function calculateBill(idMenuTable){ var fBillTotal = 0.0; var i=0; var oTable = document.getElementById(idMenuTable); var aCBTags = oTable.getElementsByTagName('INPUT'); for (i=0; i < aCBTags.length; i++) if (aCBTags.checked){ var oTR = getParentTag(aCBTags, 'TR'); var oTDPrice = oTR.getElementsByTagName('TD')[2]; fBillTotal += parseFloat(oTDPrice.firstChild.data); } } return Math.round(fBillTotal*100.0)/100.0; }CORRECT function calculateBill(idMenuTable){ var fBillTotal = 0.0; var i=0; var oTable = document.getElementById(idMenuTable); var aCBTags = oTable.getElementsByTagName('INPUT'); for (i=0; i < aCBTags.length; i++) if (aCBTags.checked){ var oTR = getParentTag(aCBTags, 'TR'); var oTDPrice = oTR.getElementsByTagName('TD')[2]; fBillTotal += parseFloat(oTDPrice.firstChild.data); } return Math.round(fBillTotal*100.0)/100.0; }3. Works here in IE7

Edited by aalbetski
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...