Type a few characters and then click on the button:
The script defines a simple form that consists of two elements: a TEXTAREA field and a button The onKeyDown event handler is used to keep track of the total number of keystrokes typed by the user in the TEXTAREA field The onKeyDown event is set to trigger each time the user types a new keystroke It calls the CountKeyStrokes() function, which adds 1 to a variable called totalKeyStrokes This variable stores a running total of the current number of keystrokes made by the user When the user clicks on the form's button, a pop-up dialog is displayed that shows the total number of keystrokes entered so far by the user NOTE The event object's event.which property specifies the ASCII value of the key pressed or the ASCII value of the mouse button that was clicked Figure 3.20 demonstrates what happens when you run the script, enter a few words in the TEXTAREA field, and then click on the form's button Figure 3.20 Using the onKeyDown event handler to count keystrokes Error Events An error event occurs whenever your JavaScripts run into an error Error events automatically trigger the onerror event By adding an onerror event handler to your scripts, you can intercept these errors and suppress them After they are suppressed, you then can either attempt to fix them programmatically or display a customized error message NOTE Unlike other event handlers, the onerror event handler is spelled in all lowercase The onerror event handler automatically receives three arguments when it is triggered: the error message itself, the URL of the Web page, and the line number in the script where the error occurred You can use the onerror event handler to respond to the error For example, you can display an alert prompt, redirect the user to another Web page, call a function, advise the user to upgrade the browser or get a specific plug-in, and so on The onerror event handler works a little differently than other event handlers and has the following syntax window.onerror=FunctionName Note that the spelling of the onerror event handler is in all lowercase and that it is not embedded within HTML tags like other event handlers Instead, it is defined within the tags as a JavaScript statement When triggered, the onerror event handler calls a function However, the matching pair of parentheses are left off of the end of the function call When used, the onerror event handler is set up in the head section of the HTML page, along with its associated function The following script demonstrates how to use the onerror event handler to display error information using a function called ErrorTrap() This function accepts three arguments that map to the three arguments passed to the onerror event handler The function uses these arguments to format three lines of text that present the error information To produce an error on the page, I deliberately added an s to the end of the word window in the HTML page's body tag windows is a not a valid browser object Script 3.17 - onError Example Welcome to my Web page NOTE Note the return true; statement at the end of the ErrorTrap function in the previous example This statement tells the browser to suppress the error Figure 3.21 shows the results of loading this script using Internet Explorer As you can see, the message written to the window tells you what the error was, where in the Web page the error occurred, and where the page is located Figure 3.21 Trapping errors with the onerror event handler The onerror event hander can also be used within HTML tags By placing the onerror event handler within a particular HTML tag, you can define different actions for error events on an object-by-object basis For example, the following JavaScript uses this technique to create an error message in the event that the specified graphic file cannot be found Script 3.18 - Another onError Example Figure 3.22 shows the results of loading the previous script An alert prompt is immediately displayed when the browser fails to locate the specified xxxx.jpg image file Figure 3.22 Using the onerror event handler to report problems when loading an image Using the onClick Event as a Decision Point Before wrapping up the first half of this afternoon, I want to show you a neat little trick using links and the onClick event handler In the example that follows, I defined a link to a Web site to which I added the onClick event Inside the event handler, I used the window.confirm() method to ask the user for confirmation before enabling the browser to load the link The window.confirm() prompt gives the user two options Clicking on OK returns a value of true, which enables the browser to load the Web page; clicking on Cancel returns a value of false, which prevents the browser from loading the specified URL Script 3.19 - Age Verification Example ptpmagazine.com NOTE TIP You can use a variation of this example to ask users whether they are sure they want to leave your Web site when they click on links Figure 3.23 shows what happens if you load the preceding page Figure 3.23 Verifying a user's age before allowing him into your Web site ... ... In this example, an alert dialog appears when the page finishes loading Notice that the event handler comes immediately after the tag and that its value is placed within quotation marks You can use any JavaScript statement as the value for the event handler... event to occur whenever a Web page is loaded by placing an onLoad event handler inside the first HTML tag as shown here: In this example, an alert dialog appears when the page finishes loading