INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

dynamic listboxes !

event handlers by jaredn
Posted: 18 Dec 00

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.

Back to Javascript FAQ Index
Back to Javascript Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close