Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Event keyword in js

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?

like image 257
Dan Avatar asked Feb 11 '11 10:02

Dan


People also ask

What is event keyword in JS?

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.

How do you call an event in JavaScript?

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.

What is an event type?

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.

Is event reserved in JavaScript?

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.


2 Answers

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 this div, the value of element.onclick is effectively:

function onclick(event) {
   foo();
}

Note how the event object is passed as parameter event to this wrapper function.

like image 139
Felix Kling Avatar answered Oct 17 '22 00:10

Felix Kling


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 the Event object of the event in question.

That argument is aptly named event for obvious reasons. http://www.w3.org/TR/html5/webappapis.html#events

like image 25
Andy E Avatar answered Oct 17 '22 00:10

Andy E