Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trace JavaScript events like onclick onblur?

Is there a way to debug or trace every JavaScript event in Internet Explorer 7?

I have a bug that prevents scrolling after text-selecting, and I have no idea which event or action creates the bug. I really want to see which events are being triggered when I move the mouse for example.

It's too much work to rewire the source and I kind of hoped there was something like a sniffer which shows me all the events that are triggered.

like image 654
Mafti Avatar asked Sep 18 '08 10:09

Mafti


4 Answers

Loop through all elements on the page which have an onXYZ function defined and then add the trace to them:

var allElements = document.all; // Is this right? Anyway, you get the idea.

for (var i in allElements) {
    if (typeof allElements[i].onblur == "function") {
        var oldFunc = allElements[i].onblur;
        allElements[i].onblur = function() {
             alert("onblur called");
             oldFunc();
        };
    }
}
like image 60
nickf Avatar answered Nov 19 '22 18:11

nickf


You might want to try Visual Studio 2008 and its feature to debug JavaScript code.

If the problem is not specific to Internet Explorer 7 but also occurs in Firefox, then another good way to debug JavaScript code is Firefox and the Firebug add-on which has a JavaScript debugger. Then you can also put console.log statements in the JavaScript code which you can then see the output of in the Console Window in Firebug, instead of using alerts which sometimes mess up the event chain.

like image 22
Michiel Borkent Avatar answered Nov 19 '22 18:11

Michiel Borkent


@[nickf] - I'm pretty sure document.all is an Internet Explorer specific extension.

You need to attach an event handler, there's no way to just 'watch' the events. A framework like jQuery of the Microsoft Ajax library will easily give you methods to add the event handlers. jQuery is nice because of its selector framework.

Then I use Firebug (Firefox extension) and put in a breakpoint. I find Firebug is a lot easier to set up and tear down than Visual Studio 2008.

like image 2
Aaron Powell Avatar answered Nov 19 '22 18:11

Aaron Powell


Borkdude said:

You might want to try Visual Studio 2008 and its feature to debug JavaScript code.

I've been hacking around event handling multiple times, and in my opinion, although classical stepping debuggers are useful to track long code runs, they're not good in tracking events. Imagine listening to mouse move events and breaking into another application on each event... So in this case, I'd strongly advise logging.

If the problem is not specific to Internet Explorer 7 but also occurs in Firefox, then another good way to debug JavaScript code is Firefox and the Firebug add-on which has a JavaScript debugger.

And there's also Firebug Lite for Internet Explorer. I didn't have a chance to use it, but it exists. :-) The downside of it is that it doesn't a fully-fledged debugger, but it has a window.console object, which is exactly what you need.

like image 1
Bartosz Leper Avatar answered Nov 19 '22 16:11

Bartosz Leper