JavaScript Events

previous next


Events are actions that can be detected by JavaScript.


Events

By using JavaScript, we have the ability to create dynamic web pages. Events are actions that can be detected by JavaScript.

Every element on a web page has certain events which can trigger JavaScript functions. For example, we can use the onClick event of a button element to indicate that a function will run when a user clicks on the button. We define the events in the HTML tags.

Examples of events:


Event Handlers

Events are normally used in combination with functions, and the function will not be executed before the event occurs!

New to HTML 4.0 was the ability to let HTML events trigger actions in the browser, like starting a JavaScript when a user clicks on an HTML element. Below is a list of the attributes that can be inserted into HTML tags to define event actions.

Attribute

The Event Occurs When ...

onAbort Loading of an image is interrupted
onBlur An element loses focus
onChange The content of a field changes
onClick Mouse clicks an object
onDblClick Mouse double-clicks an object
onError An error occurs when loading a document or an image
onFocus An element gets focus
onKeyDown A keyboard key is pressed
onKeyPress A keyboard key is pressed or held down
onKeyUp A keyboard key is released
onLoad A page or an image is finished loading
onMouseDown A mouse button is pressed
onMouseMove The mouse is moved
onMouseOut The mouse is moved off an element
onMouseOver The mouse is moved over an element
onMouseUp A mouse button is released
onReset The reset button is clicked
onResize A window or frame is resized
onSelect Text is selected
onSubmit The submit button is clicked
onUnload The user exits the page

 


onLoad and onUnload

The onLoad and onUnload events are triggered when the user enters or leaves the page.

The onLoad event is often used to check the visitor's browser type and browser version, and load the proper version of the web page based on the information.

Both the onLoad and onUnload events are also often used to deal with cookies that should be set when a user enters or leaves a page. For example, you could have a popup asking for the user's name upon his first arrival to your page. The name is then stored in a cookie. Next time the visitor arrives at your page, you could have another popup saying something like: "Welcome Driss!".


onFocus, onBlur and onChange

The onFocus, onBlur and onChange events are often used in combination with validation of form fields.

Below is an example of how to use the onChange event. The checkEmail() function will be called whenever the user changes the content of the field:

<html>
<head>

<script type=
"text/javascript">
  function checkEmail(email)
  {
    var isEmail=true
    if(email.indexOf('@')==-1)isEmail=false
    var message=isEmail?"Your Email contains @.":"Your Email does not contain @."
    alert(message)
  }
</script>

</head>
<body>
<font face="Verdana" size=2>
Type your Email address and then click outside the text field.

<form>
<input type="text" size="30" id="email" onChange="checkEmail(this.value)">
</form>

</font>
</body>

</html>

 


onSubmit

The onSubmit event is used to validate ALL form fields before submitting it.

Below is an example of how to use the onSubmit event. The checkForm() function will be called when the user clicks the submit button in the form. If the field values are not accepted, the submit should be cancelled. The function checkForm() returns either true or false. If it returns true the form will be submitted, otherwise the submit will be cancelled:

<form method="post" action="dataProcess.jsp" onSubmit="return checkForm()">

 


onMouseOver and onMouseOut

onMouseOver and onMouseOut are often used to create "animated" buttons.

Below is an example of an onMouseOver event. An alert box appears when an onMouseOver event is detected:

<a href="http://www.myjavaserver.com/~chaib/" onmouseover="alert('An onMouseOver event')">
Please, pass the mouse pointer over this link.
</a>

 


Try it

To see how HTML and JavaScript work, you can only push the submit button, or you can make your own HTML and JavaScript code.

           

 


previous next