Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Sep 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Custom error for Javascript calc

    I have a temperature calculator using javascript and i need to display a custom error message the pops up when there are errors during the conversion process. The jsp page should expect and use a query string parameter named "msg" containing the error message to be reported to the user. It should be displayed when necessary as a redirect from "convert.jsp".

    here is my calculator code :

    Code:
    <!doctype html>
    <html>
      <head>
        <meta charset="UTF-8"/>
        <title>JSP Temperature Calculator</title>
      <head>
      <body>
        <h1>Temperature Conversion</h1>
        <p>
          <input type="text" id="temperature" />
          <input type="button" id="f_to_c" value="F to C" />
          <input type="button" id="c_to_f" value="C to F" />
        </p>
        <p id="result"></p>
        <script src="convert.js"></script>
      </body>
    </html>
    and here is the jsp formula for the conversion. "aka my convert.jsp page"

    Code:
    var report = function (celsius, fahrenheit) {
        document.getElementById("result").innerHTML =
            celsius + "\xb0C = " + fahrenheit + "\xb0F";
    };
    
    document.getElementById("f_to_c").onclick = function () {
        var f = document.getElementById("temperature").value;
        report((f - 32) / 1.8, f);
    };
    
    document.getElementById("c_to_f").onclick = function () {
        var c = document.getElementById("temperature").value;
        report(c, 1.8 * c + 32);
    };
    Thank you for any input.

  • #2
    Senior Coder DanInMa's Avatar
    Join Date
    Nov 2010
    Location
    Salem,Ma
    Posts
    1,577
    Thanks
    13
    Thanked 248 Times in 248 Posts
    Ive never used jsp, but heres a start (looks like regular ole javascript to me)
    Code:
    var report = function (celsius, fahrenheit) {
    try
      {
          document.getElementById("result").innerHTML =
            celsius + "\xb0C = " + fahrenheit + "\xb0F";
      }
    catch(err)
      {
      txt="There was an error on this page.\n\n";
      txt+="Error description: " + err.message + "\n\n";
      txt+="Click OK to continue.\n\n";
      alert(txt);
      }
    };
    Last edited by DanInMa; 10-13-2013 at 03:43 AM.

  • #3
    New to the CF scene
    Join Date
    Sep 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    awesome. thank you for the help!

  • #4
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,473
    Thanks
    13
    Thanked 361 Times in 357 Posts
    Quote Originally Posted by DanInMa View Post
    (looks like regular ole javascript to me)
    that’s because it is JavaScript.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Quote Originally Posted by DanInMa View Post
    Ive never used jsp, but heres a start (looks like regular ole javascript to me)
    Code:
    var report = function (celsius, fahrenheit) {
    try
      {
          document.getElementById("result").innerHTML =
            celsius + "\xb0C = " + fahrenheit + "\xb0F";
      }
    catch(err)
      {
      txt="There was an error on this page.\n\n";
      txt+="Error description: " + err.message + "\n\n";
      txt+="Click OK to continue.\n\n";
      alert(txt);
      }
    };
    That will only throw exception when the element with id="result" does not exist and not when there is a conversion error.

    I don't see any conversion error occurring except when the inputs are not numbers. And in that case, you would just check using isNaN() to validate the inputs without using try/catch statement.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________


  •  

    Posting Permissions

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