• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint

Handling Events

Working with events in JavaScript has grown complicated over the years as event object models have changed and more functionality has been added. There are all kinds of things you can do in event handling in JavaScript. Here’s an example that reassigns event handlers on-the-fly (you’ll need version 6+ if you’re using Netscape Navigator):

(Listing 15-12.html on the web site)

<HTML> 
    <HEAD> 
        <TITLE> 
            Reassigning Event Handlers 
        </TITLE> 
        <SCRIPT LANGUAGE="JavaScript"> 
            <!--
            function alerter() 
            { 
                window.alert("You clicked the button!") 
            } 

            function reassign()
					{
					document.getElementById("button1").onclick=alerter2
					} 

            function alerter2() 
            { 
                window.alert("Here's a new message!") 
            } 
            // --> 
        </SCRIPT> 
    </HEAD> 

    <BODY> 
        <H1>Reassigning Event Handlers</H1> 
        <FORM> 
            <INPUT TYPE="BUTTON" ID="button1" ONCLICK="alerter()" VALUE="Click Me!"> 
            <INPUT TYPE="BUTTON" ONCLICK="reassign()" VALUE="Reassign event handler"> 
        </FORM> 
    </BODY> 
</HTML> 

					  


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint