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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

event handlers

dynamic listboxes !

event handlers

by  jaredn  Posted    (Edited  )
in Javascript, to associate a given function with an event via script, you must use syntax like this:

someFunc()
{
//do something
}

window.document.body.onclick = someFunc

The important thing to note is that in the line above, there are no ()'s after someFunc. The reasoning behind this is, javascript will execute whatever is on the right of the equals sign, this is because in JS, functions can be treated ad data. Suppose you wanted to pick a random event handler for your onclick event. You could use something like this:

function randomHandler()
{
//do something and define rndmhdnler as
//a function reference
return rndmhdnler
}

window.document.body.onclick = randomHandler()

it immediately, before any click occurs (whenever the intrepreter first sees the line above) runs randomHandler and returns a function reference.

This is just an example of one event handler you can register.So remember don't use ()'s unless you want the code to be run immediately.

Note: Above applies only when setting the events in a script block. If you define them inline like:

<img onmouseover="someFunc()">

notice the ()'s are there - strange, but that's just the way it is.
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top