asp.net.ph

Skip Navigation LinksHome > Abakada: Back to Basics > Dynamic HTML > Handling Events

Dynamic HTML

Previous: Understanding Events   Next: Event Bubbling


Declare a globally attached event handler

In some cases, the code to be executed when an event occurs can be written as a function as above, and directly associated to either the window or document object. In this case, the handler receives control when the given event occurs in any element in the entire document or window. For example:

<script language="JavaScript">
document.onkeypress = "getKeyCode ( )"

function getKeyCode ( ) {
   ... JavaScript statements ...
}
</script>

The code above triggers the function whenever a key is pressed anywhere at any time on the document.

When associating functions to events in this way, an alternate way is to assign the function pointer, rather than a function call, to the property as in the following example.

document.onkeypress = getKeyCode;

In that case, the parentheses following the function name in the document.onkeypress assignment are not required.

More ...
Back to top


© 2025 Reynald Nuñez and asp.net.ph. All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note