|
Event | When Triggered | Comments |
---|---|---|
onload | When the page is loaded | Placed with the body tag |
onunload | When the browser switches to a new html page | Placed with the body tag. Might be used to warn the user that they are leaving the web-site. |
onclick | When the mouse is clicked over an object | typically used with a button, checkbox, or radio button |
onmousedown | When a mouse button is pressed while the mouse cursor is over an object | not commonly used |
onmouseup | When a mouse button is released while the mouse cursor is over an object | not commonly used |
onmouseover | When the mouse cursor moves onto an object | can be used for rollovers, which are not discussed in this class |
onmousemove | When the mouse cursor moves while over an object | very costly because each pixel move by the mouse gets reported--you would be unlikely to use this event |
onmouseout | When the mouse cursor leaves an object | used to restore an element or link to its original state after a rollover |
onfocus | When an object gains the focus | might be used to select the text in a textbox |
onchange | when an object loses the focus and its value has changed | useful with a textbox or menu |
onblur | When an object loses the focus | rarely useful since typically you would use onchange |
onkeypress | When a key is pressed and released | could be used to 1) count the number of key strokes so that dashes can be automatically inserted in a phone number or social security number, 2) to move the focus to the next text element if the maximum number of characters for this textbox is exceeded, or 3) to prevent the return key from causing a form to submit its contents to the server (more about this below) |
onkeydown | When a key is pressed | rarely used |
onkeyup | When a key is released | rarely used |
onsubmit | When a form is submitted but before the form's contents are sent to the server. | The function should return true if the form's contents should be sent to the server and false if the form's contents should not be sent to the server (e.g., the form fails validation and the user needs to correct one or more entries) |
onreset | when the form is reset | return false if you do not want to reset the form |
onselect | when text is selected in a text field | used with a textbox |
// 1) newFocusObject is the form element that should get the focus // 2) the ascii code for the return key is 13 function changeReturnKey(event, newFocusObject) { if (event.keyCode == 13) { event.preventDefault(); newFocusObject.focus(); } } ... html code for a text box <form name="addr"> <input type="textbox" name="phoneNumber" size="12" maxlength="12" onkeypress="changeReturnKey(event, document.addr.email)" /> <input type="textbox" name="email" size="20" maxlength="25" onkeypress="changeReturnKey(event, document.addr.submitButton)" /> <input type="submit" name="submitButton"/> </form>
<head> <script> function printColor() { alert("color = " + document.Order.color.value); return false; } </script> </head> <body> <form name="Order" method="get" onsubmit='return printColor();'> Blue: <input type='radio' name='color' value='blue' checked="true"/> <br/> Red: <input type='radio' name='color' value='red' /> <br/> Yellow: <input type='radio' name='color' value='yellow'/> <br/> <input type="submit"/> </form> </body>