Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

432 LESSON 14: Introducing JavaScript The Meaning of this You might be a bit puzzled by the use of this as a variable name in an event han- dler. Here, this is shorthand for the current object. When you're using an event han- dler in a tag, this refers to the object represented by that tag. In the previous example, it refers to the link that the user clicked on. The advantage of using this is that it places the event in a useful context. I could use the same attribute value with any link and the code would still work as expected. It's particularly useful when you're using functions as event handlers and you want to make them easy to reuse. You'll see a lot more of this in the next lesson. At one time, using event-handler attributes to bind functions to events was the most com- mon approach, but these days, it's more common to bind events to elements in other ways. It's considered poor style to include JavaScript throughout your web pages, and using the event-handler attributes can override event bindings that are applied from JavaScript rather than in the HTML. In Lesson 15, I explain how to bind events to ele- ments without changing your markup at all.