Tải bản đầy đủ - 0 (trang)
Chapter 17. Events and Event Handling

Chapter 17. Events and Event Handling

Tải bản đầy đủ - 0trang

themissingdetailsabouteventsandeventhandling.

Unfortunately,thesedetailsaremorecomplexthantheyought

tobebecausethreedistinctandincompatibleevent-handling

modelsareinuse.[*]Thesemodelsare:

[*]Netscape4alsohaditsowndistinctandincompatibleeventmodel.Thatbrowserisnolongerwidely

deployed,however,anddocumentationofitseventmodelhasbeenremovedfromthisbook.



Theoriginaleventmodel

Thisisthesimpleevent-handlingschemethat'sbeenused

(butnotthoroughlydocumented)sofarinthisbook.Itwas

codifiedtoalimitedextentbytheHTML4standardandis

informallyconsideredpartoftheDOMLevel0API.Although

itsfeaturesarelimited,itissupportedbyallJavaScriptenabledwebbrowsersandisthereforeportable.



Thestandardeventmodel

Thispowerfulandfull-featuredeventmodelwas

standardizedbyDOMLevel2.Itissupportedbyallmodern

browsersexceptInternetExplorer.



TheInternetExplorereventmodel

ThiseventmodeloriginatedinIE4andwasextendedinIE

5.Ithassome,butnotall,oftheadvancedfeaturesofthe

standardeventmodel.AlthoughMicrosoftparticipatedin

thecreationoftheDOMLevel2eventmodelandhadplenty

oftimetoimplementthisstandardeventmodelinIE5.5

andIE6,ithasstuckwithitsproprietaryeventmodel

instead.[*]ThismeansthatJavaScriptprogrammerswho

wanttouseadvancedevent-handlingfeaturesmustwrite



specialcodeforIEbrowsers.

[*]Atthetimeofthiswriting,IE7isunderdevelopment,buttherearenoindicationsthatit

willsupportthestandardeventmodel,either.



Thischapterdocumentseachoftheseeventmodelsinturn.

Coverageofthethreeeventmodelsisfollowedbythree

sectionsthatincludeextendedexamplesofhandlingmouse,

keyboard,andonloadevents.Thechapterconcludeswitha

briefdiscussionofcreatinganddispatchingsyntheticevents.



17.1.BasicEventHandling

Inthecodeshownsofarinthisbook,eventhandlershavebeen

writtenasstringsofJavaScriptcodethatareusedasthevalues

ofcertainHTMLattributes,suchasonclick.Althoughthisisthe

keytotheoriginaleventmodel,thereareanumberof

additionaldetails,describedinthefollowingsections,thatyou

shouldunderstand.



17.1.1.EventsandEventTypes

Differentoccurrencesgeneratedifferenttypesofevents.When

theusermovesthemouseoverahyperlink,itcausesa

differenttypeofeventthanwhentheuserclicksthemouseon

thehyperlink.Eventhesameoccurrencecangeneratedifferent

typesofeventsbasedoncontext:whentheuserclicksthe

mouseoveraSubmitbutton,forexample,itgeneratesa

differenteventthanwhentheuserclicksthemouseoverthe

Resetbuttonofaform.

Intheoriginaleventmodel,aneventisanabstractioninternal

tothewebbrowser,andJavaScriptcodecannotmanipulatean

eventdirectly.Whenwespeakofaneventtypeintheoriginal

eventmodel,whatwereallymeanisthenameoftheevent

handlerthatisinvokedinresponsetotheevent.Inthismodel,

event-handlingcodeisspecifiedusingtheattributesofHTML

elements(andthecorrespondingpropertiesoftheassociated

JavaScriptobjects).Thus,ifyourapplicationneedstoknow

whentheusermovesthemouseoveraspecifichyperlink,you

usetheonmouseoverattributeofthetagthatdefinesthe

hyperlink.Iftheapplicationneedstoknowwhentheuserclicks

theSubmitbutton,youusetheonclickattributeofthe

tagthatdefinesthebuttonortheonsubmitattributeofthe


elementthatcontainsthatbutton.



Therearequiteafewdifferentevent-handlerattributesthatyou

canuseintheoriginaleventmodel.TheyarelistedinTable171,whichalsospecifieswhentheseeventhandlersaretriggered

andwhichHTMLelementssupportthehandlerattributes.

Table17-1.EventhandlersandtheHTMLelementsthatsupportthem

Handler



Triggeredwhen



Supportedby



onabort



Imageloadinginterrupted.







onblur



Elementlosesinputfocus.