My Profile

Monday, January 24, 2011

Creating an Event Handler

An alternate approach is to use an event handler to perform a script when a certain event occurs. This is best used when you want to act on the press of a button or the entry of a field.
Rather than use the <SCRIPT> tag, an event handler is inserted as an attribute to an HTML tag. Tags that support event handlers include <LINK>, <IMG>, and the form element tags.
As a basic example of an event handler, here's a common use for JavaScript: creating a back button in a page that performs just like the browser's back button. You can easily accomplish this with an event handler, as in Listing 1.4.

Listing 1.4. A simple JavaScript event handler.
<INPUT TYPE="button" VALUE="Back!" onClick="history.go(-1); return true;">

This defines a button with an event handler. The event handler is defined as an attribute of the <INPUT> tag. The attribute name is the event name-in this case, onClick. This is an event that occurs when the user clicks the mouse on an object.
In this example, a button is used to send the user back to the previous page. You could also use this technique with an image, or a simple link to the word "back!".
Because an event handler is inserted between double quotation marks, be sure to use single quotation marks to delimit any strings within the event handler.

Viewing Your Script's Output

The main tool you'll use to view the script's output is a Web browser. Currently, you should use Netscape to view the output, but other browsers may support JavaScript in the future. There's nothing special you need to do to view a script's output-just load the Web page that contains the script. You can even test JavaScript on your local computer, without uploading anything to the Web server.
Be sure you have the latest version of Netscape. Because JavaScript is still being developed, there may be major differences in the results between versions of the browser. All the examples in this guide are meant to use version 3.0 or later of Netscape Navigator, although they may work with older versions.


** If you like this post please like our page in facebook


Post a Comment


Twitter Delicious Facebook Digg Stumbleupon Favorites More