Skip to content

Instantly share code, notes, and snippets.

@Nutrox
Created January 31, 2011 21:44
Show Gist options
  • Save Nutrox/804886 to your computer and use it in GitHub Desktop.
Save Nutrox/804886 to your computer and use it in GitHub Desktop.
JavaScript - Handy getEvent() Function
// This function SHOULD be called directly by an event listener (see example).
// Works in Chrome, Firefox, MSIE, Opera and Safari.
window.getEvent = function() {
var event = getEvent.caller.arguments[0];
return event ? event : window.event;
}
//
// Example
//
function onClick() {
var event = getEvent();
console.log( event.type ); // click
}
document.onclick = onClick;
//
// Alternative (without the use of getEvent)
//
function onClick( event ) {
event = event ? event : window.event;
console.log( event.type ); // click
}
document.onclick = onClick;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment