...

View Full Version : code help useability



havey
07-08-2005, 05:06 PM
Hi i'm put together this code and i newish to JS, could I have some pointers on the code, things pointed out that could cause problems, with browser compatibility, or just smart things to do, Thanks

<SCRIPT LANGUAGE="javascript">
function addit() {
var a21 = document.form1.bi3.value;
a21 = (a21 * 14.95 + <%=c1%>);
a21 = "" + ((Math.round(a21 * 100)) / 100);
dec1 = a21.substring(a21.length-3, a21.length-2);
dec2 = a21.substring(a21.length-2, a21.length-1);
if (dec1 != '.') {
if (dec2 == '.') a21 += "0";
else a21 += ".00";
}
if (isNaN(a21) || a21==""){
document.getElementById("vv").innerHTML = "Invalid Number";
document.form1.bi3.value = 0
} else {
document.getElementById("vv").innerHTML = "$" + a21;
}
}
</SCRIPT>


</head><body...
<span style="font-weight: 700; background-color: #FFFF00"><div id="vv">$<%=c1%></div></span>

vwphillips
07-08-2005, 08:09 PM
subjective




<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>
<title></title>
</head>

<SCRIPT LANGUAGE="javascript">
var a21;

function addit() {
var a21 = document.form1.bi3.value;
if (isNaN(a21)|| a21==""){
// do first to stop calculations on NaN
obj.value = 0
document.getElementById("vv").innerHTML = "Invalid Number";
return;
}
a21 = Number(a21); // ? better than auto typing
a21 = (a21 * 14.95 + 22);
a21 = "" + ((Math.round(a21 * 100)) / 100);
dec1 = a21.substring(a21.length-3, a21.length-2);
dec2 = a21.substring(a21.length-2, a21.length-1);
if (dec1 != '.') {
if (dec2 == '.') a21 += "0";
else a21 += ".00";
}
document.getElementById("vv").innerHTML = "$"+a21;
}

function addit2(obj) {
a21 = obj.value;
if (isNaN(a21)|| a21==""){
// do first to stop calculations on NaN
obj.value = 0
document.getElementById("vv").innerHTML = "Invalid Number";
return;
}
a21 = Number(a21); // ? better than auto typing
a21 = (a21 * 14.95 + 22);
a21s=a21.toString().split('.');
if (a21s.length<2){
a21s[1]='00';
}
a21=Number((a21s[0]+'.'+a21s[1])).toFixed(2);
document.getElementById("vvvvv").innerHTML = "$"+a21.toString();
}

</SCRIPT>


</head>
<body>
<form name=form1 >
<div id="vv" ></div>
<input name="bi3" size="10" onblur="a21=this.value;addit(this);" >
<br><br>
Alternative<br>
<div id="vvvvv" ></div>
<input name="" size="10" onblur="addit2(this);" >
</form>
</body>

</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum