Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Understanding Events

Status
Not open for further replies.

UNIMENT

Programmer
Feb 15, 2002
302
US
Ever wondered why, when declaring an event handler, you must not have the parentheses?

So have I. This is what I suspect is the reason behind it:




There's a difference between events in HTML and events in pure JavaScript. When you say as a tag's attribute:

Code:
onclick="alert('I\'ve been clicked.');"

Any code within the attribute is executed using an equivalent to the
Code:
eval()
function. The reason for doing so is obvious: The code is contained within quotes, so you might as well
Code:
eval()
it. The disadvantage to this is that in Netscape (NS6 included), no Event object is passed to the function as a parameter (this will be discussed later).


Now for the pure JavaScript. When you say this:
Code:
function doWhat() {
 alert('Click!');
}
document.getElementById('myDiv').onclick=doWhat;
What happens is that you give
Code:
myDiv
's
Code:
onclick
property the value of the
Code:
doWhat
function. Now, why's that? Why not just use normal syntax?
It is normal syntax. Functions can be values, too. For instance:

Code:
function myFunction() {
 alert("I've been called!");
}
var myMethod=myFunction;
myMethod();
The above code is perfectly valid syntax. When you tell a variable to hold the value of a function (not the function's return value), you effectively create a new function. Just think now: the
Code:
onclick
variable is set to be a function.

Why would you want
Code:
onclick
to be a function? Well, quite simple. When an object gets clicked, the browser then calls the
Code:
onclick()
function (if it exists). This then executes the code that you assigned to
Code:
onclick
.


Then you see that if you want to get rid of an event handler, you can simply set the function to do nothing:
Code:
function blank() {}
document.getElementById('myDiv').onclick=blank;
Or, if you just want to do it nice and quickly, without defining a whole new named function:
Code:
document.getElementById('myDiv').onclick=function() {};
This way, you can cancel an old event handler and make it do nothing.


Now... For the details on the Event object.
In Internet Explorer, when an Event occurs, a new Object called
Code:
event
is created. Once the code being called is finished executing, the
Code:
event
Object is destroyed (In the same manner as the
Code:
this
Object).
In Netscape, however, although the
Code:
this
Object is created and destroyed the same way as in IE, no Object called
Code:
event
is created. Rather, a similar Event object is passed as an argument.

Code:
function doWhat(myEvent) {

} // Etc ...
From there, the
Code:
myEvent
Object (often simply called
Code:
e
) is manipulated in much the same manner as in IE.

The disadvantage to passing the Event object as an argument is that, if you have your event defined as an HTML attribute, it cannot be detected (to my knowledge) because you can't pass an argument to a string like that. So, in Netscape, although you can detect the
Code:
this
object in your HTML attributes, you can't detect any
Code:
event
object. Counter-intuitive? Yes. Generally, though, if you need to detect the Event Object, you will be doing it without using attribute event handlers anyway.
bluebrain.gif
blueuniment.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top