Skip to content

Instantly share code, notes, and snippets.

@matijs
Last active May 2, 2021 02:47
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matijs/9bb314a9a184b53952e2 to your computer and use it in GitHub Desktop.
Save matijs/9bb314a9a184b53952e2 to your computer and use it in GitHub Desktop.
;(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