Judas1012
May 25th, 2004, 12:27 PM
I've created a little JavaScript that takes two fields and performs a simple calculation from those two fields, then displays the result in a third field. I'm using a Mac and the calculator works fine in Safari, but fails in Internet Explorer (both Mac and Windows versions). I can't seem to figure out why.
Here is the function itself. The ugly part of the code after the initial code is to format the data to look like a currency amount.
<SCRIPT LANGUAGE="JavaScript">
<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->
<!-- Begin
function customPrice(form) {
var height = bannerorder.customHeight.value;
var length = bannerorder.customLength.value;
var cost = bannerorder.customCost.value;
var num = height * length * 2.65;
if (height == 2 && length == 4) {
alert("2 x 4 is a predefined size. Please select it from the menu to the left.");
bannerorder.customHeight.value = null;
bannerorder.customLength.value = null;
bannerorder.customCost.value = "$0.00";
bannerorder.size.focus();
break;
}
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
bannerorder.customCost.value = (((sign)?'':'-') + '$' + num + '.' + cents);
}
// End -->
</script>
...and this is how I call it (the form's name is bannerorder):
<tr align="center" valign="middle">
<td><input name="customHeight" type="text" id="customHeight" size="5" maxlength="3" onChange="customPrice(this.bannerorder)"></td>
<td><input name="customLength" type="text" id="customLength" size="5" maxlength="3" onChange="customPrice(this.bannerorder)"></td>
</tr>
</table>
<p><br>
<input name="CalcCost" type = button id="CalcCost" onClick="customPrice(this.bannerorder)" value="Calculate Cost">
The code is set to calculate the cost every time the user changes the value (onChange) or when they click the button.
Can anyone help? The error message that Internet Explorer provides on a Windows machine is:
Line: 274, 275, 279 (all the lines that call the function)
Char: 1
Error: Object expected
Code: 0
URL: http://www.(my domain).com
Here is the function itself. The ugly part of the code after the initial code is to format the data to look like a currency amount.
<SCRIPT LANGUAGE="JavaScript">
<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->
<!-- Begin
function customPrice(form) {
var height = bannerorder.customHeight.value;
var length = bannerorder.customLength.value;
var cost = bannerorder.customCost.value;
var num = height * length * 2.65;
if (height == 2 && length == 4) {
alert("2 x 4 is a predefined size. Please select it from the menu to the left.");
bannerorder.customHeight.value = null;
bannerorder.customLength.value = null;
bannerorder.customCost.value = "$0.00";
bannerorder.size.focus();
break;
}
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
bannerorder.customCost.value = (((sign)?'':'-') + '$' + num + '.' + cents);
}
// End -->
</script>
...and this is how I call it (the form's name is bannerorder):
<tr align="center" valign="middle">
<td><input name="customHeight" type="text" id="customHeight" size="5" maxlength="3" onChange="customPrice(this.bannerorder)"></td>
<td><input name="customLength" type="text" id="customLength" size="5" maxlength="3" onChange="customPrice(this.bannerorder)"></td>
</tr>
</table>
<p><br>
<input name="CalcCost" type = button id="CalcCost" onClick="customPrice(this.bannerorder)" value="Calculate Cost">
The code is set to calculate the cost every time the user changes the value (onChange) or when they click the button.
Can anyone help? The error message that Internet Explorer provides on a Windows machine is:
Line: 274, 275, 279 (all the lines that call the function)
Char: 1
Error: Object expected
Code: 0
URL: http://www.(my domain).com