The Event Object - Page 2


© Muhammad Ali Shah
Page 2

To give you an idea, I will show some code that displays mouse coordinates when a button is pressed. The following function works for Netscape 6. It calls the function clicked(e) and passes it an event object. The funciton receives the event object and extracts the values of attributes screenX and screenY.

<html>
<head>
<script language="JavaScript1.2">
function clicked(e) {
   var x = e.screenX;
   var y = e.screenY;

   alert ("Mouse at " + x + "," + y + ".");
}
</script>
</head>

<body>
<form name="test">
   <input type="button" value="Click" name="myButton" onClick="clicked(event)">
</body>
</html>

In case of Internet Explorer 5.0, there is no need to explicitly pass the event object. You can always use the event object - named event - inside any function. Thus, the above code can be modified as shown below.

<html>
<head>
<script language="JavaScript1.2">
function clicked() {
   var x = event.screenX;
   var y = event.screenY;

   alert ("Mouse at " + x + "," + y + ".");
}
</script>
</head>

<body>
<form name="test">
   <input type="button" value="Click" name="myButton" onClick="clicked()">
</body>
</html>


Last Words:

Registering event handlers is just the first step towards writing web pages that act like web applications. Understanding the event object is the next step. The difference in the event handling model of the two browsers is very annoying and requires extra work to write cross browser code. We will explore further in later articles.

Go To Page: 1 2


The copyright of the article The Event Object - Page 2 in JavaScript is owned by . Permission to republish The Event Object - Page 2 in print or online must be granted by the author in writing.

Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo