Skip to content

Instantly share code, notes, and snippets.

@pepebe
Forked from gnab/find-that-bastard.js
Created September 28, 2012 10:57
Show Gist options
  • Save pepebe/3799179 to your computer and use it in GitHub Desktop.
Save pepebe/3799179 to your computer and use it in GitHub Desktop.
JS: Find the event handler blocking events (event.preventDefault())
// Scenario: Some event handler is blocking events (event.preventDefault()) that shouldn't be blocked, i.e. because it binds to document instead of a more specific element
// 1) Place this before all other JavaScript
var originalPreventDefault = Event.prototype.preventDefault;
Event.prototype.preventDefault = function () {
// Lookup specific event you're expecting, i.e. pressing space
if (this instanceof KeyboardEvent && this.keyCode === 32) {
// This will log an error with full stack trace
make_error_to_see_stacktrace
}
originalPreventDefault.apply(this, arguments);
};
// 2) Do what's required to trigger the event, i.e. press some key combination
// 3) Look for error stacktrace in console, i.e
//
// Uncaught ReferenceError: make_error_to_see_stacktrace is not defined :22286/#2012.01.01:25
// Event.preventDefault :22286/#2012.01.01:25
// f.Event.preventDefault jquery-1.7.2.min.js:3
// $.jstree.plugin.__init.$.bind.$.proxy.data.contextmenu ... jquery.jstree.js:3681
// ...
// 4) jstree is obviously handling the event, which it shouldn't!
// 5) Surprise, surprise
//
// jquery.jstree.js:
$(document) // <- This should be the container element, not the entire document
// ...
.bind("keydown", "space"), function (e) {
// ...
e.preventDefault(); // 3681
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment