On 10 Aug 2004 14:17:33 -0700, Adam <ad********@sha w.ca> wrote:
[snip]
I did some modifications and it still worked fine, even in Netscape,
but at some point the Shipping/Handling field has ceased to display
correctly in that browser, showing a Not A Number error. (In Safari it
looks fine, and it calculates correctly in all browsers I've tested it
with.)
http://wiseowlmultimedia.com/learnin...s_orderUS.html
I'm no java genius, just a copy-paste-and-tinker boy, so I've
basically got no clue what I messed up. My fine-tooth comb has turned
up no reason I can see why it broke. Can anyone help?
[snip]
There are numerous possible causes in your code. Because of this, I
thought it easier to rewrite the script. That way, I also demonstrate some
more robust ways of accomplishing the things you were doing.
function totalItem(i, e) {
/* Get references to the form controls. */
var oP = e['Item' + i + 'Price'],
oQ = e['Item' + i + 'Quantity'],
oT = e['Item' + i + 'Total'];
/* Get the values and convert them to numbers. */
var p = +oP.value, q = +oQ.value, t;
/* Check that price and quantity are positive,
* finite numbers. If not, quit now. */
if(isNaN(p) || isNaN(q) || p < 0 ||
q < 0 || !isFinite(p) || !isFinite(p)) {return;}
/* A quantity shouldn't have a fractional
* part, so let's remove it. */
oQ.value = String(q = Math.floor(q));
/* Calculate total and round. */
t = Math.round((p * 100) * q) / 100;
/* Convert the total to a formatted
* string and update the form. */
oT.value = toCurrency(t, '$', ',');
return t;
}
function totalOrder() {
var e = document.forms['orderform'].elements,
n = 40,
t = 0,
s;
/* Loop through items 1..n, computing the item
* total and adding it to the order total. */
for(var i = 1; i <= n; ++i) {
var x = totalItem(i, e);
/* Check for an error. */
if('undefined' == typeof x) {
alert('Item ' + i + ' contains an invalid value.\n' +
'Please correct it and try again.');
e['Item' + i + 'Quantity'].focus();
return;
}
t += x;
}
/* Less than $100, add 15%; $100 or more, add 10% */
s = Math.round(t * ((t < 100) ? 0.15 : 0.1) * 100) / 100;
/* Update the final totals. */
e['OrderTotal'].value = toCurrency(t, '$', ',');
e['SH'].value = toCurrency(s, '$', ',');
e['GrandTotal'].value = toCurrency(t + s, '$', ',');
}
/* n - Number to format.
* c - Currency symbol to use.
* g - Grouping symbol.
*
* Outputs a number of the form cngnnngnnn.nn
*
* For example, toCurrency(1426 .356, '£', ',') produces
* £1,426.36
*/
function toCurrency(n, c, g) {
var s = (0 > n) ? '-' : ''; n = Math.abs(n);
var m = String(Math.rou nd(n * 100));
var j, i = '', f;
while(m.length < 3) {m = '0' + m;}
f = m.substring((j = m.length - 2));
while(j > 3) {
i = g + m.substring(j - 3, j) + i;
j -= 3;
}
i = m.substring(0, j) + i;
return s + c + i + '.' + f;
}
I've tested this briefly and everything seemed fine, but I don't
anticipate any problems.
A quick note:
<script language="JavaS cript">
<!--
The SCRIPT element requires the type attribute. This attribute makes the
language attribute redundant. Furthermore, the practice of script hiding
is obsolete. Even if a particular browser doesn't execute scripts, all
browsers now in use at least understand what a SCRIPT element is. Change
these lines to:
<script type="text/javascript">
In addition, you should really update the page to use CSS properly, and
remove all presentational HTML. That stuff was declared deprecated long
ago. It would be beneficial to your visitors to specify a print stylesheet
so that they don't print extraneous information when they make an order.
Finally, I strongly suggest that you read the group FAQ
(<URL:http://jibbering.com/faq/>), particularly sections 4.12, 4.39 (and
its link) and 4.40.
Hope that helps,
Mike
--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail