The event in JavaScript is a global variable

Source: Internet
Author: User

The only thing I can think are in fact and window.event it makes itself available when some event dispatches and Event...but that would mean this any element could get this event information if called at the same time as the event time It triggers?

The

you is Right ( window.event ), and don ' t know what the there is and more to say on that. of Course you can access global variables from everywhere (that's why they is global ).  – felix Kling jun" at 14:03  

It might be confusing. But if there was no parameter or variable in the current function that's named event , you know it must come from a hig Her scope window . And if you pass it event as, then you is actually access the parameter and not window.event . So it just works-functions work:) –felix Kling

Original: https://stackoverflow.com/questions/6426497/ Is-event-a-global-variable-that-is-accessible-everywhere-inside-the-callback-cha

------------------------------------------------------Split Line-----------------------

Event represents the Window.event object, he put this parameter into the, if you do not pass also does not matter, but to consider the browser has no window.event problem, previously seemingly FF is not;

When an event is triggered on the DOM, an event object is generated that event contains all the information related to the event. Includes the element that caused the event, the type of event, and other information related to the specific event. For example, an event object that is caused by a mouse action contains information about the location of the mouse, and the event object that is caused by the keyboard action contains information about the key that was pressed. All browsers support event objects, but they are supported in different ways.

Event objects in the DOM

A DOM-compatible browser passes an event object into the event handler. No matter what method (DOM1 or DOM2) is used to specify the event handler, the object is passed in event . Take a look at the following example.

 var btn =  document.getElementById ( "mybtn"); Btn.onclick = function (event) {console.log (Event.type); Span class= "hljs-comment" >//"click"};btn.addeventlistener ( "click", function (event) { console.log (Event.type); //"click"}, false);       

The two event handlers in this example will pop up a warning box that shows event.type the type of event represented by the property. This property always contains the type of event that is triggered, for example "click" (as with the type of event that is passed addEventListener() removeEventListener() in and in).

When you specify an event handler through an HTML attribute, the event object is saved in the variable event . Take a look at the example below.

<input type="button" value="Click Me" onclick="console.log(event.type)"/>

Providing an object in this manner allows the event HTML attribute event handler to perform the same operation as the JavaScript function.

eventThe object contains properties and methods related to the specific event that created it. The types of events that are triggered are different, and the available properties and methods are different. However, all events will have members listed in the following table.

  • bubblesThat indicates whether the event bubbled.

  • cancelableThat indicates whether the default behavior of the event can be canceled.

  • currentTargetThe element whose event handler is currently processing the event.

  • defaultPrevented, to true indicate that it has been called preventDefault() (new in DOM3-level event).

  • detail, and details about the event.

  • eventPhase, the stage for invoking the event handler: 1 for the capture phase, 2 for "on target" and 3 for the bubbling phase.

  • preventDefault(), cancels the default behavior of the event. If cancelable true So, you can use this method.

  • stopImmediatePropagation(), cancels further capture or bubbling of the event, and prevents any event handlers from being called (New in the DOM3-level event).

  • stopPropagation()To cancel further capture or bubbling of the event. If bubbles true So, you can use this method.

  • target, the objective of the event.

  • trusted, to true indicate that the event was generated by the browser. To false indicate that the event was created by the developer through JavaScript (new in the DOM3 level event).

  • typeThe type of event that was triggered.

  • view, an abstract view associated with an event, equivalent to the object in which the event occurred window .

More information can be found in the JavaScript entry story.

The event in JavaScript is a global variable

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.