Created
January 31, 2011 21:44
-
-
Save Nutrox/804886 to your computer and use it in GitHub Desktop.
JavaScript - Handy getEvent() Function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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