Previous Example-|-Next Example-|-Return to Chapter Listing

Example 12.18:
Using Scripts to Modify Event Handlers

[This example works with Netscape Navigator 3. It will not work with Internet Explorer 3.]

You can use a script to modify an event handler. There are now two different messages available for display when you click on Reset. There's the Polite message (the default), and the Snotty message (which will only display if you click on the Change Message to Snotty button first). The two big buttons modify the Reset button's onclick event handler.



In the HEAD we have this script: <SCRIPT LANGUAGE="JAVASCRIPT"> <!-- function confirmReset() { if (confirm("Are you sure you want to clear this form?")) { document.forms[0].reset() } } function confirmReset2() { if (confirm("You know, clearing this form is a really stupid thing to do!")) { document.forms[0].reset() } } //--> </SCRIPT> In the BODY of the page we have this form: <form onreset="alert('The form has been cleared and returned to its original state.')"> <input type="text" value="Here's the text" size="20"> <input name="resetButton" type="button" value="Reset" onclick="confirmReset()"><p> <input type="button" value="Change Message to Snotty" onclick="document.forms[0].resetButton.onclick=confirmReset2"><br> <input type="button" value="Change Message to Polite" onclick="document.forms[0].resetButton.onclick=confirmReset"> </form>
Previous Example-|-Next Example-|-Return to Chapter Listing