#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Location
    Pabna, Bangladesh
    Posts
    19
    Rep Power
    0

    Need some JavaScript definitions


    Hi Everybody!

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


    try_catch_statement:

    <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:

    <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.
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Location
    39N 104.28W
    Posts
    157
    Rep Power
    2
    Originally Posted by active-worker

    # 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?

    # What is “else if(isNaN(x))”? Why I should use it?
    1. txt is a variable that will be used to build a message out of strings that gets displayed. You could call it anything.

    2. "\n" is a linefeed (so "\n\n" is "skip 2 lines")

    3. "txt+=" is a shorthand syntax for "txt = txt + ...". That is, the string after the "+=" gets appended the current value of "txt". In this case, “txt+="Error description: " + err.description + "\n\n" means: take the current value of txt, append "Error description: ", then append the value of the variable, err.description (from the error event), then skip 2 lines.

    4. onClick=message() means when the button is clicked, run the function, message().

IMN logo majestic logo threadwatch logo seochat tools logo