I found this code working in Chrome, FF, and IE. However, I can't find any references to the "magic" event
keyword on the web. See this
<html>
<head>
<script type="text/javascript">
function handler(e){
alert(e);
}
</script>
</head>
<body>
<h1 onclick="handler(event);alert(0)">Click on this text</h1>
</body>
</html>
This script STOPS working if I change event
in brackets to something else. Is that a deprecated keyword?
When an event attribute is set, an implicit Function is created with event as the first argument. When the event fires, the event object is passed to that function.
element. addEventListener(event, function, useCapture); The first parameter is the type of the event (like " click " or " mousedown " or any other HTML DOM Event.) The second parameter is the function we want to call when the event occurs.
An event type is a data structure that defines the data contained in an event. When raw event data comes into the Oracle Event Processing application, the application binds that data to an event of a particular event type.
No. There are heaps of examples on stackoverflow that use event, but they're using it for clarity. When you use that code, you need to edit it.
The Event
object is automatically passed to all event handlers. If you add event handlers with addEventListener
, you can choose the parameter name (like you did in handler
). But for code attached with the onclick
attribute, you can only access the event object via the implicit variable event
.
If you want to know more about event handling in general, I suggest to read about it at quirksmode.org.
Also have a look at MDC - Event handlers:
These are properties that correspond to the HTML 'on' event attributes.
Unlike the corresponding attributes, the values of these properties are functions (or any other object implementing the EventListener interface) rather than a string. In fact, assigning an event attribute in HTML creates a wrapper function around the specified code. For example, given the following HTML:
<div onclick="foo();">click me!</div>
If
element
is a reference to thisdiv
, the value ofelement.onclick
is effectively:function onclick(event) { foo(); }
Note how the event object is passed as parameter
event
to this wrapper function.
When an event attribute is set, an implicit Function is created with event
as the first argument. When the event fires, the event object is passed to that function. There wasn't a solid definition on this behavior until HTML 5 (which is still a draft), but it's been this way for a long time in the major browsers and that's how it made it into the spec:
When an event handler's Function object is invoked, its
call()
callback must be invoked with one argument, set to theEvent
object of the event in question.
That argument is aptly named event
for obvious reasons. http://www.w3.org/TR/html5/webappapis.html#events
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With