Exception Handling in JavaScript

Below is the simple code to understand, how to handle the exception using javascript and alert to user.

<script language="javascript" type="text/javascript">
        window.onload = intAll;
        function intAll(){
            var ans = window.prompt("Enter a number","");
            try{
                if (isNaN(ans) || ans < 0)
                    throw new Error("Invalid Number");
                else    
                    alert("Square root of the number " + ans + " is " + Math.sqrt(ans));
            }
            catch (err){
                alert(err.message);
            }
        } 
</script>

#error-handling

How to check JavaScript disable in browser?

It is very easy to detect whether JavaScript is disable on browser or not?

You just need to put <noscript> tag in your HTML body. For example,

<noscript>
<div>Javascript is disable on your browser</div>
</noscript>

If JavaScript is disable then above HTML text will be print on html document.

 

 

#javascript-disable-checking