Sams Teach Yourself HTML, CSS & JavaScript Web Publishing in One Hour a Day

(singke) #1
ptg16476052

Events 493

17


Event Handler When It’s Called


onfocus Whenever a visitor enters a specified form field


onload Whenever a page and all of its images have finished loading


onmouseover Whenever a visitor places the mouse cursor over a specified object


onselect Whenever a visitor selects the contents of a specified field


onsubmit Whenever a visitor submits a specified form


onunload Whenever the current web page is changed


First, let me explain how to bind an event using HTML attributes. All the event handlers
listed in Table 17.4 can be used as attributes for tags that respond to them. For example,
the onload handler is associated with the body tag. As you know, JavaScript code is
executed as soon as it is encountered. Suppose you want to write a script that modifies all
the links on a page. If that script is executed before all the links have been loaded, it will
miss some of the links. Fortunately, there’s a solution to this problem. The onload event
does not occur until the entire page has loaded, so you can put the code that modifies the
links into a function and then bind it to the page’s onload event. Here’s a page that uses
onload:


<!DOCTYPE html>




Modifying Links with JavaScript






This page contains a script tag, and that script tag contains a single function declaration.
The function, linkModifier(), changes the href attribute of all the links on the page to
http://example.com/. To access the links, it uses document.links, which is a reference

Free download pdf