Results 1 to 6 of 6

Thread: [RESOLVED] Javascript: Object Expected PART 2

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2006
    Posts
    25

    [RESOLVED] Javascript: Object Expected PART 2

    As mentioned in my previous post with the same title, I am trying to move all of my javascript to external files. This hasn't been so easy. The last script was made to be written internally and I just rearranged it so it could be read as an external file. In doing so, I didn't realize that some of the things i wrote were case sensitive.

    My next issue won't be so simple, because in this case, I am actuall take the internal script which is layed out just fine, and moving it to an external file. When I do this, I get the same non descript error "Object Expected"

    Here is the document at hand:

    Code:
    <script language="javascript" type="text/javascript">
    <!--
    	var isIE4 = false;
    	var cache;
    
    	if(navigator.appName.indexOf("Microsoft") != -1  &&  parseInt(navigator.appVersion) >= 4)
    	 isIE4 = true;
    
    	function ReadDollarDecision(whichVal) {
    		dwnPayFld 	= document.frmCalc.downpay;
    		price 	= document.frmCalc.price;
    
    		if(whichVal < ReadDollarField(dwnPayFld)) {
    			dwnPayFld.value = price.value;
    		}
    	}
    
    	function CheckFloatField(field) {
    	   var val = field.value;
    
    	   // lop off trailing "0"s after a decimal point first
    	   if(val.indexOf(".") != -1) {
    	      while(val.charAt(val.length-1) == "0")
    	         val = val.substring(0,val.length-1);
    	      if(val.charAt(val.length-1) == ".")
    	         val = val.substring(0,val.length-1);
    	   }
    
    	   if("" + parseFloat(val) != val)
    	      field.value = field.defaultValue;
    	}
    
    
    	function CheckIntField(field) {
    	   var val = field.value;
    	   if("" + parseInt(val) != val)
    	      field.value = field.defaultValue;
    	}
    
    
    	function CheckDollarField(field) {
    	   var flt = ReadDollarField(field);
    	   if(isNaN(flt))
    	      field.value = cache;
    	   else {
    	      str = FloatToDollarString(flt);
    	      field.value = str;
    	   }
    	}
    
    
    	function ReadDollarField(field) {
    	   var str = field.value;
    	   if(str.charAt(0) == "$")
    	      str = str.substring(1, str.length);
    
    	   var pos = str.lastIndexOf(",");
    	   while(pos != -1) {
    	      str = str.substring(0,pos) + str.substring(pos+1, str.length);
    	      pos = str.lastIndexOf(",", pos);
    	   }
    
    	   return parseFloat(str);
    	}
    
    
    	function FloatToDollarString(flt) {
    	   // round off to nearest dollar
    	   var str = "" + Math.round(flt)
    
    	   // add commas
    	   pos = str.length;  // str.indexOf(".");
    	   pos -= 4;
    	   while(pos >= 0) {
    	      str = str.substring(0,pos+1) + "," + str.substring(pos+1, str.length);
    	      pos -= 3;
    	   }
    
    	   return str;
    	}
    
    
    	function recalcTermMonths(frm) {
    	   var tYr = parseFloat(frm.termYears.value);
    	   var tMon = Math.round(tYr * 12.0);
    	   tYr = parseFloat(tMon) / 12.0;
    	   frm.termYears.value = "" + tYr;
    	   frm.termMonths.value = "" + tMon;
    	}
    
    
    	function recalcTermYears(frm) {
    	   var tMon = parseInt(frm.termMonths.value);
    	   var tYr = parseFloat(tMon) / 12.0;
    	   frm.termYears.value = "" + tYr;
    	   frm.termMonths.value = "" + tMon;
    	}
    
    
    	function RecalcMonthlyPay(frm) {
    	   var Principle  = ReadDollarField(frm.price) - ReadDollarField(frm.downpay);
    	   var AnnualInt  = parseFloat(frm.intYear.value);
    	   var MonthlyInt = AnnualInt / (12.0 * 100.0);
    	   var LenMonths  = parseInt(frm.termMonths.value);
    
    	   if(MonthlyInt == 0)
    	      var MonthlyPay = Principle / LenMonths;
    	   else
    	      var MonthlyPay = Principle * ( MonthlyInt / ( 1 - Math.pow((1 + MonthlyInt), -LenMonths) ) );
    	   MonthlyPay = Math.round(MonthlyPay * 100) / 100;
    
    	   frm.payMonth.value = FloatToDollarString(MonthlyPay);
    	}
    
    
    	function RecalcDownPay(frm) {
    	   var AnnualInt  = parseFloat(frm.intYear.value);
    	   var MonthlyInt = AnnualInt / (12.0 * 100.0);
    	   var LenMonths  = parseInt(frm.termMonths.value);
    	   var MonthlyPay = ReadDollarField(frm.payMonth);
    	   var Principle  = ReadDollarField(frm.price) - ReadDollarField(frm.downpay);
    	   var OldDownPay = ReadDollarField(frm.downpay);
    	   var EffPrinciple
    
    	   if(MonthlyInt == 0)
    	      EffPrinciple = MonthlyPay * LenMonths;
    	   else
    	      EffPrinciple = MonthlyPay * ((1 - Math.pow((1 + MonthlyInt), -LenMonths)) / MonthlyInt);
    
    	   var NewDownPay = OldDownPay + (Principle - EffPrinciple);
    	   frm.downpay.value = "" + NewDownPay;
    	   CheckDollarField(frm.downpay);
    
    	   RecalcDownPayPerc(frm);
    	   RecalcMonthlyPay(frm);
    	}
    
    
    	function RecalcDownPayPerc(frm) {
    	   var HomePrice  = ReadDollarField(frm.price);
    	   var DownPay = ReadDollarField(frm.downpay);
    	   var DownPayPerc = 100 * DownPay / HomePrice;
    
    	   if(DownPayPerc >= 0  &&  DownPayPerc <= 100) {
    	      var DownPayPercStr = "" + DownPayPerc;
    
    	      var pos = DownPayPercStr.indexOf(".");
    	      if(DownPayPercStr.length > pos + 4)
    	         DownPayPercStr = DownPayPercStr.substring(0,pos+4);
    
    	      frm.downpayperc.value = DownPayPercStr;
    	   }
    	   else if(DownPayPerc < 0) {
    	      frm.downpayperc.value = "3";
    	      RecalcDownPayAmount(frm);
    	   }
    	   else {
    	      frm.downpayperc.value = "3";
    	      RecalcDownPayAmount(frm);
    	   }
    	}
    
    
    	function RecalcDownPayAmount(frm) {
    	   var HomePrice  = ReadDollarField(frm.price);
    	   var DownPayPerc = parseFloat(frm.downpayperc.value);
    	   if(DownPayPerc < 0) {
    	      frm.downpayperc.value = "0";
    	      RecalcDownPayAmount(frm)
    	   }
    	   else if(DownPayPerc > 100) {
    	      frm.downpayperc.value = "100";
    	      RecalcDownPayAmount(frm)
    	   }
    	   else {
    	      var DownPay = HomePrice * DownPayPerc / 100;
    	      DownPay = FloatToDollarString(DownPay);
    	      frm.downpay.value = "" + DownPay;
    	   }
    	}
    -->
    </script>
    
    
    <h1>Home Mortgage Calculator</h1>
    
    <center><form name="frmCalc" action="#">
    <table border="0" width="320">
    <tr><td>Home Price: </td><td>$ <input type="text" name="price" id="price" size="8" value="250,000" onchange="CheckDollarField(this); ReadDollarDecision(ReadDollarField(this)); RecalcMonthlyPay(this.form); RecalcDownPayPerc(this.form);" onfocus="cache=this.value" class="textfields" /></td></tr>
    
    <tr><td>Down Payment:</td><td class="text">$ <input type="text" name="downpay" id="downpay" size="8" value="" onchange="CheckDollarField(this);RecalcDownPayPerc(this.form);RecalcMonthlyPay(this.form)" onfocus="cache=this.value" class="textfields" /> or <input type="text" name="downpayperc" id="downpayperc" size="2" value="3" onchange="CheckFloatField(this);RecalcDownPayAmount(this.form);RecalcMonthlyPay(this.form)" onfocus="cache=this.value" class="textfields" /> %</td></tr>
    
    <tr><td>Loan Term:</td><td class="text"><input type="text" name="termYears" id="termYears" size="2" value="30" onchange="CheckFloatField(this);recalcTermMonths(this.form);RecalcMonthlyPay(this.form)" onfocus="cache=this.value" class="textfields" /> 
    
    years or 
    
    <input type="text" name="termMonths" id="termMonths" size="4" value="360" onchange="CheckIntField(this);recalcTermYears(this.form);RecalcMonthlyPay(this.form)" onfocus="cache=this.value" class="textfields" /> months</td></tr>
    
    <tr><td>Annual Interest Rate:</td><td><input type="text" name="intYear" id="intYear" size="5" value="5.75%" onchange="CheckFloatField(this);RecalcMonthlyPay(this.form)" onfocus="cache=this.value" class="textfields" /> %</td></tr>
    
    <tr><td>Monthly Payment:</td>
    <td>$ <input type="text" name="payMonth" id="payMonth" size="6" readonly="readonly" value="0" onchange="CheckDollarField(this);RecalcDownPay(this.form)" onfocus="cache=this.value" /></td></tr>
    </table>
    
    <br />
    <a href="#" onclick="RecalcMonthlyPay(document.frmCalc); return(false);" onfocus="this.blur()">CALCULATE</a>
    </form>
    Last edited by niclipse; Feb 3rd, 2006 at 12:59 PM. Reason: resolved

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Javascript: Object Expected PART 2

    Just use Firefox for testing, it will give you meaningful messages. Then come back.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jan 2006
    Posts
    25

    Re: Javascript: Object Expected PART 2

    RecalcMonthlyPay is not defined, is what firefox says.

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Javascript: Object Expected PART 2

    My Fox says the code is fine. (I just copied & pasted what you posted, added html, head and body tags, and opened the file.)

    Are you sure you're linking to the JavaScript file correctly?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jan 2006
    Posts
    25

    Re: Javascript: Object Expected PART 2

    yup. it works great when used internally, but when i try to move it to the external file, it does haywire. I set it all up correctly...

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jan 2006
    Posts
    25

    [RESOLVED] Javascript: Object Expected PART 2

    IE says, "Line 65, Object Expected." Here is line 65:
    Code:
    <a href="#" onclick="RecalcMonthlyPay(document.frmCalc); return(false);" onfocus="this.blur()"><img src="_images/calculator.gif" border="0" alt="Calculate the above mortgage scenario" /></a>
    </form>
    Is it because the onclick refers to "document"? Does moving javascript to an external file mean that "document" needs to be changed to something else?

    EDIT--------
    Turns out i never uploaded the external file to the server. I am so sorry for my ignorance.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width