The document discusses several topics related to JavaScript including error handling, form validation, and animation. It describes three types of errors in programming - syntax errors, runtime errors, and logical errors. It explains how to use try, catch, and finally statements to handle exceptions in JavaScript. It also discusses how JavaScript can be used to validate forms on the client-side before submitting to the server. Finally, it provides an overview of how to create animations in JavaScript using functions like setTimeout and setInterval to move DOM elements according to logical equations.
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
16 views
Advance Js
The document discusses several topics related to JavaScript including error handling, form validation, and animation. It describes three types of errors in programming - syntax errors, runtime errors, and logical errors. It explains how to use try, catch, and finally statements to handle exceptions in JavaScript. It also discusses how JavaScript can be used to validate forms on the client-side before submitting to the server. Finally, it provides an overview of how to create animations in JavaScript using functions like setTimeout and setInterval to move DOM elements according to logical equations.
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 6
Errors & Exceptions Handling
• There are three types of errors in programming:
(a) Syntax Errors, (b) Runtime Errors, (c) Logical Errors • Syntax Errors:-Syntax errors, also called parsing errors, occur at compile time in traditional programming languages and at interpret time in JavaScript. • For example, the following line causes a syntax error because it is missing a closing parenthesis. <script type = "text/javascript"> window.print(; </script> Runtime Errors • Runtime Errors:-Runtime errors, also called exceptions, occur during execution (after compilation/interpretation). • For example, the following line causes a runtime error because here the syntax is correct, but at runtime, it is trying to call a method that does not exist. <script type = "text/javascript"> window.printme(); </script> Logical Errors • Logic errors can be the most difficult type of errors to track down. These errors are not the result of a syntax or runtime error. Instead, they occur when you make a mistake in the logic that drives your script and you do not get the result you expected. • You cannot catch those errors, because it depends on your business requirement what type of logic you want to put in your program. The try...catch...finally Statement • The latest versions of JavaScript added exception handling capabilities. JavaScript implements the try...catch...finally construct as well as the throw operator to handle exceptions. • You can catch programmer-generated and runtime exceptions, but you cannot catch JavaScript syntax <script >errors. <!-- try { // Code to run [break;] } catch ( e ) { // Code to run if an exception occurs [break;] } [ finally { // Code that is always executed regardless of // an exception occurring }] //--> </script> • The try block must be followed by either exactly one catch block or one finally block (or one of both). When an exception occurs in the try block, the exception is placed in e and the catch block is executed. The optional finally block executes unconditionally after try/catch. • The throw Statement:-You can use throw statement to raise your built-in exceptions or your customized exceptions. Later these exceptions can be captured and you can take an appropriate action. Form Validation • Form validation normally used to occur at the server, after the client had entered all the necessary data and then pressed the Submit button. If the data entered by a client was incorrect or was simply missing, the server would have to send all the data back to the client and request that the form be resubmitted with correct information. This was really a lengthy process which used to put a lot of burden on the server. • JavaScript provides a way to validate form's data on the client's computer before sending it to the web server. Form validation generally performs two functions. • Basic Validation − First of all, the form must be checked to make sure all the mandatory fields are filled in. It would require just a loop through each field in the form and check for data. • Data Format Validation − Secondly, the data that is entered must be checked for correct form and value. Your code must include appropriate logic to test correctness of data. Animation • JavaScript to create a complex animation having, but not limited to, the following elements − • Fireworks • Fade Effect • Roll-in or Roll-out • Page-in or Page-out • Object movements • You might be interested in existing JavaScript based animation library: Script.Aculo.us. • This tutorial provides a basic understanding of how to use JavaScript to create an animation. • JavaScript can be used to move a number of DOM elements (<img />, <div> or any other HTML element) around the page according to some sort of pattern determined by a logical equation or function. • JavaScript provides the following two functions to be frequently used in animation programs. • setTimeout( function, duration) − This function calls function after duration milliseconds from now. • setInterval(function, duration) − This function calls function after every duration milliseconds. • clearTimeout(setTimeout_variable) − This function calls clears any timer set by the setTimeout() functions. • Syntax // Set distance from left edge of the screen. object.style.left = distance in pixels or points; or // Set distance from top edge of the screen. object.style.top = distance in pixels or points;