• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 196
  • Last Modified:

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>
0
clive1
Asked:
clive1
  • 4
  • 3
  • 2
  • +1
1 Solution
 
oubelkasCommented:
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.

0
 
Michel PlungjanIT ExpertCommented:
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
0
 
HazemCommented:
Hi;

at the begining of the ortgage_form.htm
files after the <HTML> tag add this:
<HEAD>
<script language="javascript" src="myscripts.js"></script>
</HEAD>

that's all;
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
Michel PlungjanIT ExpertCommented:
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
0
 
oubelkasCommented:
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.
0
 
Michel PlungjanIT ExpertCommented:
Joseph: It is Clive, not Dive and no need to shout or get exited ;-)
Michel
0
 
oubelkasCommented:
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.

0
 
HazemCommented:
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.
0
 
Michel PlungjanIT ExpertCommented:
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

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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now