Event Driven Programming

In Java console applications the flow of the application is controlled by the developer. As a developer, you prompt the user to select from a list of pre-defined options. Additionally, the user can only select from those options when they are presented. In other words, as a developer, you have complete control of when and how methods are executed. For instance, when prompting a user for input, you prompt them one piece of information at a time. It is easy to validate user input in this model. Creating rich and engaging user interfaces turns some control of application flow over to the user. A web page "listens" for user actions like button clicks, key presses, mouse movements and more. These are called events. As a developer you can respond to these events. They allow developers to create a much more interactive experience for the user. An event driven application also allows a user to complete forms, click buttons, or links in any order. They may not complete an entire form before clicking the submit button. As a developer, you now need to include logic that accounts for the flexibility given to a user. Dealing with user input includes validating their input before submitting forms. You need to think about how a user will interact with your site, and write your JavaScript to respond accordingly.

Updated

Responding to Events

  1. Responding to Events: an overview (Apprentice): An event is something that happens on a page, such as a click, and your code responds to it by giving the browser a function to run each time it happens.
  2. Imperative (Apprentice): In the imperative style, your JavaScript finds an element and attaches a handler to it with addEventListener, so the HTML holds no JavaScript at all.

Common Events

  1. Non-Form Events (Apprentice): Any element on a page can respond to the mouse, such as a click or a press of a mouse button, so even a plain div or table row can react to the user.
  2. Form Events (Apprentice): Form controls send their own events, for the moments a visitor types, changes a value, moves between fields or sends the form, so you can pick the one that fires exactly when you need it.
  3. Canceling Events (Journeyman): A handler can cancel what the browser would do next, such as sending a form that failed validation, by calling preventDefault() on the event object the browser passes in.