Events

JavaScript is used to run client code in the browser as a reaction to a user taking action or something on the page happening. These actions are triggered events to which you can subscribe by writing event-handling functions. There are dozens of events, such as the page loading, the user clicking a button, the Tab key being pressed, a hyperlink being selected, and the browser being resized.

Events are based on a publisher-subscriber pattern. Objects already expose events that will be triggered when certain things happen. You can then write code to subscribe to these events. In doing so, your code will then be called when the event bubbles up.

Let’s take a look at an example. Imagine that you have the markup shown in Listing 18.2. This ...

Get Microsoft Visual Studio 2015 Unleashed, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.