JavaScript Events are items that transpire based on an action. A document event is the loading of an HTML document. A form event is the clicking on a button. Events are objects with properties. Event Properties
JavaScript defines five types of events which are form, image, image map, link, and window events. Events are associated with HTML tags. The definitions of the events described below are as follows: Form Events
Image Events
Image Map Events
Link Events
Window Events
| |
Events are associated with HTML tags. The definitions of the events described below are as follows:
The events for each HTML tag are as follows:
Event handlers are created as follows:
onEvent = "Code to handle the event"
The following example demonstrates its use:
<a href="independent/index.html" target="_top" onMouseOver="window.status='To Independent Technologies Section' ;return true" onMouseOut="window.status='';return true"><img SRC="gifs/greenindependentbutton2.gif" ALT="Independent Technologies" VSPACE=3 BORDER=0 height=35 width=120></a><BR>
As you can see, the event handling attribute is included in the HTML tag. When multiple statements are included in the event handling code, the statements are separated by a semicolon.
The following example can be used to redirect a link to another location:
<HTML> <HEAD> <TITLE>Using functions as event handlers</TITLE> <SCRIPT LANGUAGE="JavaScript"> <!-- function nameDefined(ckie,nme) { var splitValues var i for (i=0;i<ckie.length;++i) { splitValues=ckie[i].split("=") if (splitValues[0]==nme) return true } return false } function delBlanks(strng) { var result="" var i var chrn for (i=0;i<strng.length;++i) { chrn=strng.charAt(i) if (chrn!=" ") result += chrn } return result } function getCookieValue(ckie,nme) { var splitValues var i for(i=0;i<ckie.length;++i) { splitValues=ckie[i].split("=") if(splitValues[0]==nme) return splitValues[1] } return "" } function testCookie(cname, cvalue) { //Tests to see if the cookie var cookie=document.cookie //with the name and value var chkdCookie=delBlanks(cookie) //are on the client computer var nvpair=chkdCookie.split(";") if(nameDefined(nvpair,cname)) //See if the name is in any pair { tvalue=getCookieValue(nvpair,cname) //Gets the value of the cookie if (tvalue == cvalue) return true else return false } else return false } function redirectLink() { if (testCookie("javahere", "yes")) { window.location="javahere.html" } else{ var futdate = new Date() var expdate = futdate.getTime() expdate += 3600*1000 //expires in 1 hour(milliseconds) futdate.setTime(expdate) var newCookie="javahere=yes; path=/;" newCookie += " expires=" + futdate.toGMTString() window.document.cookie=newCookie window.location="javanot.html" } } //--> </SCRIPT> </HEAD> <BODY> <H3>Using an event handler to direct a link based on a cookie value</H3> <P> <A NAME="Here" onClick="return redirectLink()">See if you've been here.</A> </P> </BODY> </HTML>
联系客服