Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
;(function(handlers) {
if (!handlers) {
throw new Error('Nothing to handle');
}
document.documentElement.addEventListener('click', function(event) {
var handler = event.originalTarget.getAttribute('data-handler');
if (!handler) {
// nothing to do
return;
}
if (event.originalTarget.tagName === 'A' && (event.metaKey || event.ctrlKey || event.shiftKey)) {
// honour default behaviour on <a>s when using modifier keys when clicking
// meta / ctrl open in new tab
// shift opens in a new window
return;
}
if (typeof handlers[handler] === 'function') {
handlers[handler].call(event.originalTarget, event);
}
else {
if (console && console.log) {
console.log('unknown handler \'%\'s on %o', handler, event.originalTarget);
}
}
});
}({
'example-handler': function(event) {
event.preventDefault();
// I'm the example handler
}
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.