Link to home
Start Free TrialLog in
Avatar of clive1
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(this, false)"></td>
  </tr>
  <tr>
    <td>Interest: </td>
    <td></td>
    <td><input TYPE="TEXT" NAME="interest" SIZE="9" onChange="computeField(this, false)">%</td>
  </tr>
  <tr>
    <td>Duration:</td>
    <td></td>
    <td><input TYPE="TEXT" NAME="years" SIZE="9" onChange="computeField(this, false)">yrs</td>
  </tr>
  <tr>
    <td><input TYPE="reset" VALUE="Reset" onClick="clearForm(this.form)"></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(this, 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.length == 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.length != 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.length == 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.value * (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.principal, 100, 10000000, "Principal"))
            {
                  form.payment.value = "Invalid";
                  return;
            }

            form.payment.value = Math.round(100.0*(form.principal.value * pow * i) / (pow - 1)) / 100.0
      }
}


function clearForm(form)
{
    form.years.value = "";
    form.interest.value = "";
    form.principal.value = "";
      form.payment.value = "";
}

//-->
</script>
Avatar of oubelkas
oubelkas

You must include the file in your html page and then you can call up the functions, just use :

<script language="javascript" src="myscripts.js"></script>

of course the source must exist.

J.

Avatar of Michel Plungjan
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
ASKER CERTIFIED SOLUTION
Avatar of Hazem
Hazem

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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.
Joseph: It is Clive, not Dive and no need to shout or get exited ;-)
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.

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.
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

Avatar of clive1

ASKER

Thank's for the answer Hazem. Appreciate the help from Michel and Oulbekas as well.
Many thanks,
Clive