Here we are going to learn following :
- Making simple functions and calling them
- List of events
- Accessing a field value
- Date operationsUsing try-catch to handle exceptions
First we will learn how we may validate a field in a html form:
Listing 1: Making simple functions
- We make a unction by first writing keyword function and then we write function name which can be any name.Here in our case we took name as fun
- Now in body we called the function fun().The function is called when the body loads.This is ensured by calling the function on an event onload
- onclick : If applied on a button then this event get activated when that button is clicked
- onmousemove : This event take place when the mouse moves.
- onkeypress: This event take place when a key is pressed.
- onload : This event take place when an object is loaded
- onblur : This event take place when an object lose focus.Like you may apply it on textbox
- onsubmit : This event take place when for is submitted
Listing 2: Access field values
- We made a function called reveal.
- This function uses document.getElementById(“revealme”).We used getElementById and pass “revealme” as argument.It tells that document has to use the object having id as revealme(this will be clear when we explain the body part).We now used the value function to determine the value of that field
- Now we write the body part.We will make two fields
- Now we make a submit button and use the onsubmit event to call the reveal method.This ensures that reveal method is called when the submit button is clicked
Listing 3: Moving to particular location
- We used location.href which help us to move to a particular location.Here we used it to move to google
Now we learn how to obtain date information.
Listing 4 : Date information
- First we make a date object.We make a variable call today and use new Date() to make a variable containing the date.
- Now we make use of alert to display the date
Now we will learn exception handling.This is very useful when you want the application to run even if a certain piece of code fails to run properly.This is done making use of try-catch statement
Listing 5: Exception handling
Here, we have used the try-catch statement.We have added a alertme. Now since alertme function does not exist so an exception occurs and the control now goes to catch statement and a message is displayed with the error which occurred.
This is all for this article.See you next time with some more exciting article