1. Technology

JavaScript By Example

Events: 5. Type of Event

By

While everything relating to events is stored in one of two different places depending on the browser we can by appropriate naming of our function argument remove the need to test which of the two places we need to reference in order to find out what type of event has been triggered.

JavaScript passes the current event into functions while JScript used by Internet Explorer uses a global event object to store that information. By naming our argument "event" the code within our function can access the event object regardless of which of the two is used. In the case of the event type (mouseover, click, keypress etc.) the property is named the same in both cases and so we avoid all together the need to test for which of the two is applicable. Of course things will not be as simple when we need to access information about the event that isn't stored in the same property in both variants.

Any event triggered function can easily find out what event triggered it by calling the following function.


getEventType = function(event) {
  return event.type;
};

  1. About.com
  2. Technology
  3. JavaScript
  4. Javascript Tutorials
  5. JavaScript By Example
  6. Events
  7. JavaScript By Example - Events: Type of Event

©2014 About.com. All rights reserved.