I am creating a food menu (for school). It is all coded out and works fine in IE but the javascript will not work in Firefox. Searched for some tips on google and w3schools but no luck...
Javascript:
function Item(anItem, aPrice, aDescription, anImage) {
this._anItem = anItem;
this._aPrice = aPrice;
this._aDescription = aDescription;
this._anImage = anImage;
}
Item.prototype._anItem;
Item.prototype._aPrice;
Item.prototype._aDescription;
Item.prototype._anImage;
Item.prototype.getanItem = function() {
return this._anItem;
}
Item.prototype.getaPrice = function() {
return this._aPrice;
}
Item.prototype.getaDescription = function() {
return this._aDescription;
}
Item.prototype.getanImage = function() {
return this._anImage;
}
function roundNumber(num, dec) {
var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
return result;
}
function importXML()
{
if (document.implementation && document.implementation.createDocument)
{
xmlDoc = document.implementation.createDocument("", "", null);
xmlDoc.onload = importMenu;
}
else if (window.ActiveXObject)
{
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.onreadystatechange = function () {
if (xmlDoc.readyState == 4) importMenu()
};
}
else
{
alert('Your browser can\'t handle this script');
return;
}
xmlDoc.load("Menu.xml");
}
function importMenu()
{
var x = xmlDoc.getElementsByTagName('food');
for (i=0;i<x.length;i++)
{
var arrItem = new Array();
for (j=0;j<x.childNodes.length;j++)
{
if (x.childNodes[j].nodeType != 1) continue;
arrItem[j] = x.childNodes[j].firstChild.nodeValue;
}
arrItems[arrItems.length] = new Item(arrItem[0], arrItem[1], arrItem[2], arrItem[3]);
}
}
var arrItems = new Array();
importXML();
for(var i = 0; i < arrItems.length; i++)
{
document.writeln("<p>");
document.writeln(arrItems.getanItem());
document.writeln("<br/>");
document.writeln("$");
document.writeln(arrItems.getaPrice());
document.writeln("<br/>");
document.writeln(arrItems.getaDescription());
document.writeln("<br/>");
document.write("Quantity: ");
document.write("<select id = number");
document.write(i);
document.write("><option value = '0'>0</option><option value = '1'>1</option><option value = '2'>2</option><option value = '3'>3</option><option value = '4'>4</option><option value = '5'>5</option><option value = '6'>6</option><option value = '7'>7</option><option value = '8'>8</option><option value = '9'>9</option><option value = '10'>10</option>");
document.write("</select>");
document.writeln("<br/>");
document.write("<img src = ");
document.write(arrItems.getanImage());
document.write(" />");
document.writeln("<br/>");
document.writeln("</p>");
document.writeln("<hr/>");
}
function price()
{
var intTotal = 0;
for(var i = 0; i < arrItems.length; i++)
{
var aprice = "number";
aprice += i;
var location = document.getElementById(aprice).value;
intTotal += parseFloat(location) * parseFloat(arrItems.getaPrice());
}
window.alert("Your final bill comes to: $" + roundNumber(intTotal, 2));
}
XHTML:
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Matthew Daly's and Chris Miller's JavaScript Page</title>
<script type="text/javascript">
<!--
//-->
</script>
<link rel="stylesheet" type="text/css" href="CSS.css" />
</head>
<body>
<h1>Welcome To Shenanigans!</h1><br/>
<h2>Please choose items from our menu</h2>
<script src="jslib4.js">
</script>
<input type = 'Button' name = 'Submit' value= 'Submit Order' id='submit' onclick = price() />
<hr/>
© Matthew Daly, Chris Miller.
</body>
</html>
Javascript:
function Item(anItem, aPrice, aDescription, anImage) {
this._anItem = anItem;
this._aPrice = aPrice;
this._aDescription = aDescription;
this._anImage = anImage;
}
Item.prototype._anItem;
Item.prototype._aPrice;
Item.prototype._aDescription;
Item.prototype._anImage;
Item.prototype.getanItem = function() {
return this._anItem;
}
Item.prototype.getaPrice = function() {
return this._aPrice;
}
Item.prototype.getaDescription = function() {
return this._aDescription;
}
Item.prototype.getanImage = function() {
return this._anImage;
}
function roundNumber(num, dec) {
var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
return result;
}
function importXML()
{
if (document.implementation && document.implementation.createDocument)
{
xmlDoc = document.implementation.createDocument("", "", null);
xmlDoc.onload = importMenu;
}
else if (window.ActiveXObject)
{
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.onreadystatechange = function () {
if (xmlDoc.readyState == 4) importMenu()
};
}
else
{
alert('Your browser can\'t handle this script');
return;
}
xmlDoc.load("Menu.xml");
}
function importMenu()
{
var x = xmlDoc.getElementsByTagName('food');
for (i=0;i<x.length;i++)
{
var arrItem = new Array();
for (j=0;j<x.childNodes.length;j++)
{
if (x.childNodes[j].nodeType != 1) continue;
arrItem[j] = x.childNodes[j].firstChild.nodeValue;
}
arrItems[arrItems.length] = new Item(arrItem[0], arrItem[1], arrItem[2], arrItem[3]);
}
}
var arrItems = new Array();
importXML();
for(var i = 0; i < arrItems.length; i++)
{
document.writeln("<p>");
document.writeln(arrItems.getanItem());
document.writeln("<br/>");
document.writeln("$");
document.writeln(arrItems.getaPrice());
document.writeln("<br/>");
document.writeln(arrItems.getaDescription());
document.writeln("<br/>");
document.write("Quantity: ");
document.write("<select id = number");
document.write(i);
document.write("><option value = '0'>0</option><option value = '1'>1</option><option value = '2'>2</option><option value = '3'>3</option><option value = '4'>4</option><option value = '5'>5</option><option value = '6'>6</option><option value = '7'>7</option><option value = '8'>8</option><option value = '9'>9</option><option value = '10'>10</option>");
document.write("</select>");
document.writeln("<br/>");
document.write("<img src = ");
document.write(arrItems.getanImage());
document.write(" />");
document.writeln("<br/>");
document.writeln("</p>");
document.writeln("<hr/>");
}
function price()
{
var intTotal = 0;
for(var i = 0; i < arrItems.length; i++)
{
var aprice = "number";
aprice += i;
var location = document.getElementById(aprice).value;
intTotal += parseFloat(location) * parseFloat(arrItems.getaPrice());
}
window.alert("Your final bill comes to: $" + roundNumber(intTotal, 2));
}
XHTML:
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Matthew Daly's and Chris Miller's JavaScript Page</title>
<script type="text/javascript">
<!--
//-->
</script>
<link rel="stylesheet" type="text/css" href="CSS.css" />
</head>
<body>
<h1>Welcome To Shenanigans!</h1><br/>
<h2>Please choose items from our menu</h2>
<script src="jslib4.js">
</script>
<input type = 'Button' name = 'Submit' value= 'Submit Order' id='submit' onclick = price() />
<hr/>
© Matthew Daly, Chris Miller.
</body>
</html>