JS References

JS Objects
JS HTML DOM

Suscriber with us



Receive HTML?

Syndicate

JS Events PDF Print E-mail

The complete coolest thing about Javascript is the ability to create dynamic web pages that increase user interaction, making the visitor feel like the website is almost coming alive right before their eyes.

The building blocks of an interactive web page is the Javascript event system. An event in Javascript is something that happens with or on the web page. A few example of events:

  • A mouse click
  • The web page loading
  • Mousing over a hot spot on the web page, also known as hovering
  • Selecting an input box in an HTML form
  • A keystroke

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

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 John Doe!".

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:

<input type="text" size="30" id="email" onchange="checkEmail()">

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="xxx.htm" 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.phpguru.biz.com" onmouseover="alert('An onMouseOver event');return false">
<img src="phpguru.gif" width="100" height="30">
</a>

onabort Event :

The onabort event occurs when loading of an image is aborted.

Syntax

onabort="SomeJavaScriptCode"

Parameter Description

SomeJavaScriptCode 

Required. Specifies a JavaScript to be executed when the event occurs. 

Supported by the following HTML tags:

<img>

Supported by the following JavaScript objects:

image

Example 1

In this example an alert box will be displayed if the loading of the image is aborted:

<img src="image_w3default.gif" onabort="alert('Error: Loading of the image was aborted')">

Example 2

In this example we will call a function  if the loading of the image is aborted:

<html>
<head>
<script type="text/javascript">
function abortImage()
{
alert('Error: Loading of the image was aborted');
}
</script>
</head>
<body>
<img src="image_w3default.gif" onabort="abortImage()">
</body>
</html>





Digg!Del.icio.us!Google!Live!Facebook!Slashdot!Technorati!StumbleUpon!Newsvine!Yahoo!Free social bookmarking plugins and extensions for Joomla! websites!
Last Updated ( Wednesday, 19 March 2008 )
 
< Prev   Next >

Polls

Which is the best Scripting language?