SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2012
    Location
    Pabna, Bangladesh
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need some JavaScript definitions

    Hi Everybody!

    Good day
    I like to know some JavaScript definitions are following:


    try_catch_statement:

    Code HTML4Strict:
    <html>
    <head>
    <script type="text/javascript">
    var txt="";
    function message()
    {
    try
      {
      adddlert("Welcome guest!");
      }
    catch(err)
      {
      txt="There was an error on this page.\n\n";
      txt+="Error description: " + err.description + "\n\n";
      txt+="Click OK to continue.\n\n";
      alert(txt);
      }
    }
    </script>
    </head>
     
    <body>
    <input type="button" value="View message" onClick="message()" />
    </body>
     
    </html>

    Please see the codes above and give me the following answer:

    # What is “txt”? Why I should use this?
    # What is “There was an error on this page.\n\n”? What is .\n\n?
    # What is “txt+="Error description: " + err.description + "\n\n";” here? I mean
    txt+,
    Error description:,
    err.description and
    "\n\n"?
    # What is “onClick="message()” as well?



    throw_statement:

    Code HTML4Strict:
    <html>
    <body>
    <script type="text/javascript">
    var x=prompt("Enter a number between 0 and 10:","");
    try
    { 
    if(x>10)
      {
      throw "Err1";
      }
    else if(x<0)
      {
      throw "Err2";
      }
    else if(isNaN(x))
      {
      throw "Err3";
      }
    }
    catch(er)
    {
    if(er=="Err1")
      {
      alert("Error! The value is too high");
      }
    if(er=="Err2")
      {
      alert("Error! The value is too low");
      }
    if(er=="Err3")
      {
      alert("Error! The value is not a number");
      }
    }
    </script>
    </body>
    </html>

    # What is “else if(isNaN(x))”? Why I should use it?

    No more, I am waiting for your kind response.

    Thank you.
    Last edited by TechnoBear; Apr 18, 2013 at 03:35. Reason: Code tags added

  2. #2
    Grüße aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,890
    Mentioned
    211 Post(s)
    Tagged
    12 Thread(s)
    Hi,

    Quote Originally Posted by foysalu9 View Post
    What is “txt”? Why I should use this?
    A string. It contains an error message which is passed to alert() in the case that the first function cannot be executed successfully.

    Quote Originally Posted by foysalu9 View Post
    What is .\n\n?
    Two newline characters.

    Quote Originally Posted by foysalu9 View Post
    What is “txt+="Error description: " + err.description + "\n\n";” here?
    An example of string concatenation.

    Quote Originally Posted by foysalu9 View Post
    What is “onClick="message()” as well?
    An inline event handler, which executes a function ("message") in the case that the user clicks on the element it is bound to.

    Quote Originally Posted by foysalu9 View Post
    What is “else if(isNaN(x))”? Why I should use it?
    A function which checks to see if "x" is Not a Number.

    Hope that helps.

    Might I also suggest that you check out a couple of beginner JavaScript tutorials, such as this one.


Tags for this Thread

Bookmarks

Posting Permissions

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