Click to See Complete Forum and Search --> : problems with a script!


arran19
08-04-2003, 09:05 AM
hi guys,
right i have a problem, the following script works fine for me in IE6 but anything prior to that i cant get it going, i get a NAN value! can someone have a look at it for me and let me know whats happening?

<script language="JavaScript">

function JSRecalc()
{ form = document.arran

var index1 = form.Size.selectedIndex;
var index2 = form.Door.selectedIndex;
var index6 = form.delivery.selectedIndex;

{
var Size = form.Size.options[index1].value;
var Door = form.Door.options[index2].value;
var delivery = form.delivery.options[index6].value;
}

palletopt=Math.max(eval(Door)*eval(Size))
price="14.90"
indiv=Math.max(eval(Door)*eval(price))
TotalCost=Math.round(eval(delivery)*eval(Door)+eval(palletopt)+eval(indiv))

form.TotalCost.value=TotalCost
form.palletopt.value=palletopt
form.price.value=price
form.indiv.value=indiv

}


</script>
<script for="jsscalc" event="onClick">
JSRecalc
</script>
&nbsp; </font>
<form action="cgi-bin/mailform.pl" name="arran" method="POST">
<center>
<table bgcolor="#C2C5D6" width="421" border="1" bordercolor="#000000" align="center" cellpadding="18" cellspacing="0">
<tr>
<td width="381" height="231" class="messageBox">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="45%" class="messageBox">&nbsp;</td>
<td width="47%"><font face="Arial, Helvetica, sans-serif">&nbsp;
</font></td>
<td width="6%" class="messageBox">&nbsp;</td>
<td width="2%"><font face="Arial, Helvetica, sans-serif">&nbsp;
</font></td>
</tr>
<tr>
<td height="43" class="messageBox">Destination
Postcode</td>
<td><font face="Arial, Helvetica, sans-serif">
<select name="delivery" id="delivery" onBlur="JSRecalc()" onChange="JSRecalc()">
<option value="0" selected></option>
<option value="14.90">OX</option>
<option value="27.80">PA 1-19</option>
<option value="14.90">PE</option>
<option value="20.80">SE</option>
<option value="14.90">SG</option>
<option value="14.90">SK</option>
<option value="14.90">SL</option>
<option value="14.90">SM</option>
<option value="14.90">SN</option>
<option value="14.90">SO</option>
<option value="14.90">SP</option>
<option value="14.90">SR</option>
<option value="14.90">SS</option>
<option value="14.90">ST</option>
<option value="20.80">SW</option>
<option value="27.80">SY</option>
<option value="14.90">TA</option>
<option value="27.80">TD</option>
<option value="14.90">TF</option>
<option value="14.90">TN</option>
<option value="21.10">TQ</option>
<option value="14.90">WV</option>
<option value="14.90">YO</option>
</select>
</font></td>
<td class="messageBox">&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td height="43" class="messageBox"><font face="Arial, Helvetica, sans-serif"><b>Your
Delivery Option&nbsp;</b></font></td>
<td><font face="Arial, Helvetica, sans-serif">
<select name="Size" size="1" onChange="JSRecalc()" onBlur="JSRecalc()">
<option value="0"></option>
<option value="-7.0">Half Pallet Economy</option>
<option value="5">Half Pallet Over-Night</option>
<option value="20">Full Pallet Over-Night</option>
</select>
</font></td>
<td class="messageBox">&nbsp;</td>
<td><font face="Arial, Helvetica, sans-serif">&nbsp;
</font></td>
</tr>
<tr>
<td height="43" class="messageBox"><b><font face="Arial, Helvetica, sans-serif">No
Of Pallets</font></b></td>
<td><select name="Door" onChange="JSRecalc()" onBlur="JSRecalc()">
<option value="0" selected></option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select></td>
<td class="messageBox">&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td height="43" class="messageBox"><font face="Arial, Helvetica, sans-serif"><b>Total
Price</b></font></td>
<td class="messageBox">&pound;
<input type="text" size="8" name="TotalCost"></td>
<td class="messageBox">&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td height="43" class="messageBox"><input name="palletopt" type="hidden" value="">
<input name="price" type="hidden" id="price" value="">
<input name="indiv" type="hidden" id="indiv">
</td>
<td>&nbsp;</td>
<td class="messageBox">&nbsp;</td>
<td>&nbsp;</td>
</tr>
</table>
</table>
</center>
</form></td>

gil davis
08-05-2003, 09:25 AM
Instead of "eval()" you should use "parseFloat()" to change strings to numbers. The "eval()" evaluates a string of JavaScript code without reference to a particular object.

From Netscape Javascript Reference:
The argument of the eval function is a string. If the string represents an expression, eval evaluates the expression. If the argument represents one or more JavaScript statements, eval performs the statements. Do not call eval to evaluate an arithmetic expression; JavaScript evaluates arithmetic expressions automatically.

Since your string (the value form a form object) is not an expresion, it should do nothing to the string. You have probably been helped up to now by Micro$oft's loose adherence to proper syntax.