• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 12. Handling Events > Introduction to Event Handlers

12.1. Introduction to Event Handlers

We have been talking about events since Chapter 1, “Introduction to JavaScript,” because events are inherently part of almost all Web pages and they make the pages interactive and dynamic. JavaScript events are asynchronous, meaning that they can happen at any time. They are actions that are initiated by a user visiting a Web page; for example, if the user submits a form or moves the mouse over a link or an image, he may trigger an event.[1] When an event occurs, JavaScript can execute code in response to the user's action. As shown in previous examples, if the user presses the submit button, JavaScript may check to see if a form was filled out properly; or if the mouse moves over a link, JavaScript may replace one image with a new one. JavaScript's response to one of these user-initiated events is called event handling. If the user presses a button, for example, JavaScript may handle the event by calling a function that will perform some designated task, such as to open a new window or bring a window into focus or submit a fillout form.

[1] An event is initiated by a user. The event itself may be blur, click, change, or the like. The event handler is the event preceded with “on”. For example, onBlur and onClick are attributes of an HTML tag, and are used to handle the event for which they are named.

JavaScript event handlers are not enclosed between <script></script> tags. Event handlers are attributes of HTML tags (specified in the HTML 4 specification). If the event is associated with a form tag, then it will be an attribute of the <form> tag, and if associated with a link, it will be an attribute of the <a href> tag, and so on. The string that is assigned to the event handler is the command that will be executed when the event is triggered by the user. The command is a JavaScript built-in function or user-defined function. The function will be called when the event is triggered.


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint