Skip to content

Instantly share code, notes, and snippets.

@cconger
Created September 30, 2013 22:09
Show Gist options
  • Save cconger/6771040 to your computer and use it in GitHub Desktop.
Save cconger/6771040 to your computer and use it in GitHub Desktop.
Intercept onclick handlers and wrap them with your own intercept code.
//Assume this was written before you got here.
document.body.onclick = function(){ alert(this) };
// Here's what you'd do.
function wrapper(elem, fn) {
return function() {
//You can put your intercept code here.
console.log('intercepted');
//Then call the original function with the usual arguments it would expect.
return fn.apply(elem, arguments);
}
}
if (!!document.body.onclick) {
document.body.onclick = wrapper(document.body, document.body.onclick);
}
//Now when you click on the body your console.log will run before the original onclick handler.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment