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.