943,772 Members | Top Members by Rank

Ad:
Mar 9th, 2008
0

debugging?

Expand Post »
hey im really new with javascript.
can someone tell me whats wrong with this?

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html>
  3. <head>
  4. <meta http-equiv="content-type" content="text/html; charset=windows-1250">
  5. <title>calculate distance and slope between two points</title>
  6.  
  7. <script language="javascript">
  8. <!--
  9.  
  10. var x1=0;
  11. var y1=0;
  12. var x2=0;
  13. var y2=0;
  14. var xdif=0;
  15. var ydif=0;
  16. var xsldif=0;
  17. var xdifsquared=0;
  18. var ydifsquared=0;
  19. var undersquare=0;
  20. var distance=0;
  21. var slope=0;
  22.  
  23. function calculate()
  24. {
  25.  
  26. x1=document.calcform.inpx1.value;
  27. y1=document.calcform.inpy1.value;
  28. x2=document.calcform.inpx2.value;
  29. y2=document.calcform.inpy2.value;
  30.  
  31. xdif=x2-x1;
  32. ydif=y2-y1;
  33.  
  34. xdifsquared=xdif*xdif;
  35. ydifsquared=ydif*ydif;
  36.  
  37. undersquare=xdifsquared+ydifsquared;
  38.  
  39. distance=Math.sqrt(undersquare);
  40. slope=ydif/xdif;
  41.  
  42. //-->
  43. }
  44.  
  45.  
  46. </script>
  47. </head>
  48. <body>
  49.  
  50. <p><img src="distanceformula.gif" border="1"> </p>
  51. <p><img src="slope.gif" border="1"></p>
  52. <form name="calcform">
  53. <p>point 1: (
  54. <input type="text" name="inpx1" size="5">
  55. ,
  56. <input type="text" name="inpy1" size="5">
  57. ) <br>
  58. point 2: (
  59. <input type="text" name="inpx2" size="5">
  60. ,
  61. <input type="text" name="inpy2" size="5">
  62. )<br>
  63. </p>
  64. <button type="submit" onClick="calculate();">Calculate!</button>
  65. <p>
  66. <strong>distance:</strong>
  67.  
  68. <script language="javascript">document.write(distance);</script>
  69.  
  70. </p>
  71. <p><strong>slope:</strong><br>
  72. <script language="javascript">document.write(slopetop);</script>
  73.  
  74. <br>
  75. ----- = <script language="javascript">document.write(slopefinal);</script><br>
  76.  
  77. <script language="javascript">document.write(slopebottom);</script>
  78. <br>
  79. </p>
  80. </form>
  81.  
  82.  
  83.  
  84.  
  85. </body>
  86. </html>


thanks so much,
Jason
Similar Threads
Reputation Points: 16
Solved Threads: 0
Light Poster
jasondrey13 is offline Offline
41 posts
since Jul 2005
Mar 9th, 2008
1

Re: debugging?

Where have you declared 'slopetop', 'slopebottom' and 'slopefinal'? And document.write() is evaluated as the page is being rendered. Plus Javascript is a client side technology; as soon as a page is submitted, what you get is a new page and the state maintained in Javascript variables is reset.

You need to dynamically update the value of a span or div element to get this code working as expected. Something like:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
  2. "http://www.w3.org/TR/html4/strict.dtd">
  3. <html>
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  6. <meta http-equiv="Expires" content="0" /> <!-- disable caching -->
  7. <title>Example</title>
  8. <script type="text/javascript">
  9. function calculate(frm) {
  10. if(!frm)
  11. return(false);
  12. var elems = frm.elements;
  13. var x1 = parseInt(elems['inpx1'].value, 10);
  14. var x2 = parseInt(elems['inpx2'].value, 10);
  15. var y1 = parseInt(elems['inpy1'].value, 10);
  16. var y2 = parseInt(elems['inpy2'].value, 10);
  17. var e = elems['result1'];
  18. alert("(" + x1 + "," + y1 + ")" + " and (" + x2 + "," + y2 + ")");
  19. var distance = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
  20. e.value = isNaN(distance) ? 'Invalid input' : distance;
  21. }
  22. </script>
  23. </head>
  24. <body>
  25. <form action="#">
  26. <div>(<input type="text" name="inpx1" size="5">,<input type="text" name="inpy1" size="5">)</div>
  27. <br>
  28. <div>(<input type="text" name="inpx2" size="5">,<input type="text" name="inpy2" size="5">)</div>
  29. <br>
  30. <input type="button" onclick="return calculate(this.form);" value="Calculate!">
  31. <br><br>
  32. <div>Distance: <input name="result1" readonly="readonly"></div>
  33. </form>
  34. </body>
  35. </html>
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Mar 9th, 2008
0

Re: debugging?

thanks!
Reputation Points: 16
Solved Threads: 0
Light Poster
jasondrey13 is offline Offline
41 posts
since Jul 2005
Mar 9th, 2008
0

Re: debugging?

You were having a hard time debugging this simple snippet because maybe you were testing this code in IE; for debugging use Firefox. It provides good debugging support given it has error console (Tools -> Error Console). To try it out, run your previous snippet which you just posted in Firefox and look at the error console; the errors would be right there.

If you are up to it, you can even try the Firebug plugin which provides full blown debugging. And you can be pretty sure that if something works in Firefox will also work in IE.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Embedding a PHPBB forum into your website : Problems/Solutions
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Forms with AJAX





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC