clive1
asked on
Setting up myscripts.js files
Hello. I would like to know how to call a Java script functions from a 'myscripst.js' file and use it in the mortgage_form.htm page. The pages are supplied below. I have read that this is not difficult but I can no seem to get it to work.
Thank you in advance
Clive Foyster
/\/\/\/\/\/\/\/\/\/\/\/\ = mortgage_form.htm
<table>
<tr>
<td><form method="POST">
<b><p>Mortgage Calculator</b></p>
</form>
</td>
</tr>
<tr>
<td>Principal: </td>
<td>$ </td>
<td><input TYPE="TEXT" NAME="principal" SIZE="9" onChange="computeField(thi s, false)"></td>
</tr>
<tr>
<td>Interest: </td>
<td></td>
<td><input TYPE="TEXT" NAME="interest" SIZE="9" onChange="computeField(thi s, false)">%</td>
</tr>
<tr>
<td>Duration:</td>
<td></td>
<td><input TYPE="TEXT" NAME="years" SIZE="9" onChange="computeField(thi s, false)">yrs</td>
</tr>
<tr>
<td><input TYPE="reset" VALUE="Reset" onClick="clearForm(this.fo rm)"></td>
<td></td>
<td><input TYPE="button" VALUE="Calculate" onClick="computeForm(this. form, true)"></td>
</tr>
<tr>
<td>Payment:</td>
<td>$</td>
<td><input TYPE="TEXT" NAME="payment" SIZE="9" onChange="computeField(thi s, false)">/mth</td>
</tr>
</table>
\/\/\/\/\/\/\/\/\/\= myscripts.js
script for mortgage calculation
<script LANGUAGE="JavaScript" name="mortage_calc">
<!--
function checkNumber(input, min, max, msg)
{
msg = msg + " field has invalid data: " + input.value;
var str = input.value;
for (var i = 0; i < str.length; i++)
{
var ch = str.substring(i, i + 1)
if ((ch < "0" || "9" < ch) && ch != '.')
{
alert(msg);
return false;
}
}
var num = parseFloat(str)
if (num < min || max < num)
{
alert(msg + "\n must be between " + min + " and " + max);
return false;
}
input.value = str;
return true;
}
function computeField(input, buttonFlag)
{
if (input.value != null && input.value.length != 0)
input.value = "" + eval(input.value);
computeForm(input.form, buttonFlag);
}
function computeForm(form, buttonFlag)
{
if((form.years.value == null || form.years.value.length == 0) ||
(form.interest.value == null || form.interest.value.length == 0) ||
((form.principal.value == null || form.principal.value.lengt h == 0) &&
(form.payment.value == null || form.payment.value.length == 0)))
{
if(buttonFlag == true)
alert("Please supply enough information to calculate.")
return;
}
if(form.years.value != null && form.years.value.length != 0 &&
form.interest.value != null && form.interest.value.length != 0 &&
form.principal.value != null && form.principal.value.lengt h != 0 &&
form.payment.value != null && form.payment.value.length != 0)
form.payment.value = "";
var i = form.interest.value / 1200.0;
var pow = 1;
for (var j = 0; j < form.years.value*12; j++)
pow = pow * (1 + i);
if(form.principal.value == null || form.principal.value.lengt h == 0)
{
if (!checkNumber(form.years, 1, 30, "# of years") ||
!checkNumber(form.interest , .001, 99, "Interest") ||
!checkNumber(form.payment, 1, 100000, "Payment"))
{
form.principal.value = "Invalid";
return;
}
form.principal.value = Math.round((form.payment.v alue * (pow-1) ) / (pow * i))
}
else if(form.payment.value == null || form.payment.value.length == 0)
{
if (!checkNumber(form.years, 1, 30, "# of years") ||
!checkNumber(form.interest , .001, 99, "Interest") ||
!checkNumber(form.principa l, 100, 10000000, "Principal"))
{
form.payment.value = "Invalid";
return;
}
form.payment.value = Math.round(100.0*(form.pri ncipal.val ue * pow * i) / (pow - 1)) / 100.0
}
}
function clearForm(form)
{
form.years.value = "";
form.interest.value = "";
form.principal.value = "";
form.payment.value = "";
}
//-->
</script>
Thank you in advance
Clive Foyster
/\/\/\/\/\/\/\/\/\/\/\/\ = mortgage_form.htm
<table>
<tr>
<td><form method="POST">
<b><p>Mortgage Calculator</b></p>
</form>
</td>
</tr>
<tr>
<td>Principal: </td>
<td>$ </td>
<td><input TYPE="TEXT" NAME="principal" SIZE="9" onChange="computeField(thi
</tr>
<tr>
<td>Interest: </td>
<td></td>
<td><input TYPE="TEXT" NAME="interest" SIZE="9" onChange="computeField(thi
</tr>
<tr>
<td>Duration:</td>
<td></td>
<td><input TYPE="TEXT" NAME="years" SIZE="9" onChange="computeField(thi
</tr>
<tr>
<td><input TYPE="reset" VALUE="Reset" onClick="clearForm(this.fo
<td></td>
<td><input TYPE="button" VALUE="Calculate" onClick="computeForm(this.
</tr>
<tr>
<td>Payment:</td>
<td>$</td>
<td><input TYPE="TEXT" NAME="payment" SIZE="9" onChange="computeField(thi
</tr>
</table>
\/\/\/\/\/\/\/\/\/\= myscripts.js
script for mortgage calculation
<script LANGUAGE="JavaScript" name="mortage_calc">
<!--
function checkNumber(input, min, max, msg)
{
msg = msg + " field has invalid data: " + input.value;
var str = input.value;
for (var i = 0; i < str.length; i++)
{
var ch = str.substring(i, i + 1)
if ((ch < "0" || "9" < ch) && ch != '.')
{
alert(msg);
return false;
}
}
var num = parseFloat(str)
if (num < min || max < num)
{
alert(msg + "\n must be between " + min + " and " + max);
return false;
}
input.value = str;
return true;
}
function computeField(input, buttonFlag)
{
if (input.value != null && input.value.length != 0)
input.value = "" + eval(input.value);
computeForm(input.form, buttonFlag);
}
function computeForm(form, buttonFlag)
{
if((form.years.value == null || form.years.value.length == 0) ||
(form.interest.value == null || form.interest.value.length
((form.principal.value == null || form.principal.value.lengt
(form.payment.value == null || form.payment.value.length == 0)))
{
if(buttonFlag == true)
alert("Please supply enough information to calculate.")
return;
}
if(form.years.value != null && form.years.value.length != 0 &&
form.interest.value != null && form.interest.value.length
form.principal.value != null && form.principal.value.lengt
form.payment.value != null && form.payment.value.length != 0)
form.payment.value = "";
var i = form.interest.value / 1200.0;
var pow = 1;
for (var j = 0; j < form.years.value*12; j++)
pow = pow * (1 + i);
if(form.principal.value == null || form.principal.value.lengt
{
if (!checkNumber(form.years, 1, 30, "# of years") ||
!checkNumber(form.interest
!checkNumber(form.payment,
{
form.principal.value = "Invalid";
return;
}
form.principal.value = Math.round((form.payment.v
}
else if(form.payment.value == null || form.payment.value.length == 0)
{
if (!checkNumber(form.years, 1, 30, "# of years") ||
!checkNumber(form.interest
!checkNumber(form.principa
{
form.payment.value = "Invalid";
return;
}
form.payment.value = Math.round(100.0*(form.pri
}
}
function clearForm(form)
{
form.years.value = "";
form.interest.value = "";
form.principal.value = "";
form.payment.value = "";
}
//-->
</script>
Also if your server is IIS3 or some other server that does not default understand js files, you will need to amend the mime type settings so .js is associated with
application/x-javascript
Michel
application/x-javascript
Michel
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hazem: This is the second time you answer a question where a comment already has answered it and the person commenting is waiting for the person who asked the question to either ask for more info or to say, thank, go ahead and post as an answer.
Please spend a little more time looking at the way we do this before you start hunting points by giving unrealted answers or using code already posted in a previous comment like here.
Michel
Please spend a little more time looking at the way we do this before you start hunting points by giving unrealted answers or using code already posted in a previous comment like here.
Michel
YEAH, just what Michel says, pay attention to the other comments Hazem. As you can see I already SAID SO!!
Dive1> reject the answer of Hazem since it has already been said....
J.
Dive1> reject the answer of Hazem since it has already been said....
J.
Joseph: It is Clive, not Dive and no need to shout or get exited ;-)
Michel
Michel
Oh, sorry, just got uncontroled.....pleaze excuse me.....just can't stand the fact people keep answering questions if the are already commented.
and WHOOPS! Also sorry for misspelling the name....it just looked like dive because the c and l are as if they are sticked together and therefore look like a d.
Can you forgive me, C L I V E? ;)
J.
and WHOOPS! Also sorry for misspelling the name....it just looked like dive because the c and l are as if they are sticked together and therefore look like a d.
Can you forgive me, C L I V E? ;)
J.
Hi;
There is an error in the myscripts.js file:
u should remove
<script LANGUAGE="JavaScript" name="mortage_calc">
:
:
:
</script>
just list the function and variables u want in the file.
There is an error in the myscripts.js file:
u should remove
<script LANGUAGE="JavaScript" name="mortage_calc">
:
:
:
</script>
just list the function and variables u want in the file.
Hazem is correct. js files may NOT contain any html and the <!-- --> to hide the javascript are needless anyway since the file will not be loaded if javascript is not active or present..
Michel
Michel
ASKER
Thank's for the answer Hazem. Appreciate the help from Michel and Oulbekas as well.
Many thanks,
Clive
Many thanks,
Clive
<script language="javascript" src="myscripts.js"></scrip
of course the source must exist.
J.