Created
September 30, 2013 22:09
-
-
Save cconger/6771040 to your computer and use it in GitHub Desktop.
Intercept onclick handlers and wrap them with your own intercept code.
This file contains 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
//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