Using ButtonsWe've seen buttons since as far back as Chapter 1. Here's an example that displays an alert box using an <INPUT> type button's ONCLICK event attribute that we saw in that chapter: <HTML> <HEAD> <TITLE> Executing Scripts in Response to User Action </TITLE> <SCRIPT LANGUAGE="JavaScript"> <!-- function alerter() { window.alert("You clicked the button!") } // --> </SCRIPT> </HEAD> <BODY> <H1>Executing Scripts in Response to User Action</H1> <FORM> <INPUT TYPE="BUTTON" ONCLICK="alerter()" VALUE="Click Me!"> </FORM> </BODY> </HTML> Here's the same script using a <BUTTON> type element instead (if you're using Netscape Navigator, you'll need version 6+ to use the <BUTTON> element): (Listing 12-09.html on the web site) <HTML> <HEAD> <TITLE> Executing Scripts in Response to User Action </TITLE> <SCRIPT LANGUAGE="JavaScript"> <!-- function alerter() { window.alert("You clicked the button!") } // --> </SCRIPT> </HEAD> <BODY> <H1>Executing Scripts in Response to User Action</H1> <FORM> <BUTTON ONCLICK="alerter()">Click Me!</BUTTON> </FORM> </BODY> </HTML> That's the way most buttons are used in JavaScript, but you can also click a button directly from your code, as we'll see next . |