简体   繁体   English

如何跟踪像onclick onblur这样的JavaScript事件?

[英]How to trace JavaScript events like onclick onblur?

Is there a way to debug or trace every JavaScript event in Internet Explorer 7? 有没有办法调试或跟踪Internet Explorer 7中的每个JavaScript事件?

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. 重新连接源是太多的工作,我希望有一些像嗅探器一样向我展示所有被触发的事件。

Loop through all elements on the page which have an onXYZ function defined and then add the trace to them: 遍历页面上定义了onXYZ函数的所有元素,然后将跟踪添加到它们:

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();
        };
    }
}

You might want to try Visual Studio 2008 and its feature to debug JavaScript code. 您可能希望尝试使用Visual Studio 2008及其功能来调试JavaScript代码。

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. 如果问题不是特定于Internet Explorer 7但也发生在Firefox中,那么调试JavaScript代码的另一个好方法是Firefox和带有JavaScript调试器的Firebug插件。 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. 然后你也可以在JavaScript代码中放入console.log语句,然后你可以在Firebug的控制台窗口中查看输出,而不是使用有时会搞乱事件链的警报。

@[nickf] - I'm pretty sure document.all is an Internet Explorer specific extension. @ [nickf] - 我很确定document.all是一个特定于Internet Explorer的扩展。

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. 像Microsoft Ajax库的jQuery这样的框架将很容易为您提供添加事件处理程序的方法。 jQuery is nice because of its selector framework. jQuery很好,因为它的选择器框架。

Then I use Firebug (Firefox extension) and put in a breakpoint. 然后我使用Firebug(Firefox扩展)并输入断点。 I find Firebug is a lot easier to set up and tear down than Visual Studio 2008. 我发现Firebug比Visual Studio 2008更易于设置和拆卸。

Borkdude said: Borkdude说:

You might want to try Visual Studio 2008 and its feature to debug JavaScript code. 您可能希望尝试使用Visual Studio 2008及其功能来调试JavaScript代码。

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. 如果问题不是特定于Internet Explorer 7但也发生在Firefox中,那么调试JavaScript代码的另一个好方法是Firefox和带有JavaScript调试器的Firebug插件。

And there's also Firebug Lite for Internet Explorer. 还有用于Internet Explorer的Firebug Lite 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. :-)它的缺点是它不是一个完全成熟的调试器,但它有一个window.console对象,这正是你需要的。

它是基本的,但你可以在触发某些东西时粘贴警报或document.write调用。

I am not sure on the exact code (it has been a while since I wrote complex JavaScript code), but you could enumerate through all of the controls on the form and attach an event that outputs something when the event is triggered. 我不确定确切的代码(自编写复杂的JavaScript代码以来已经有一段时间了),但您可以枚举表单上的所有控件并附加一个事件,该事件在触发事件时输出内容。

You could even use anonymous functions to wrap the necessary information for identifying which event was triggering. 您甚至可以使用匿名函数来包装必要的信息,以识别触发的事件。

The obvious way would be to set up some alerts for various events something like: 显而易见的方法是为各种事件设置一些警报,例如:

element.onclick = function () { alert('Click event'); }

Otherwise you have a less intrusive option of inserting your alerts into the dom somewhere. 否则,您可以选择将警报插入到某处的dom中。

But, seriously consider using a library like jQuery to implement your functionality. 但是,认真考虑使用像jQuery这样的库来实现您的功能。 Lots of the cross-browser issues are solved problems and you don't need to solve them again. 很多跨浏览器问题都解决了问题,您无需再次解决它们。 I am not sure exactly of the functionality you are trying to achieve but there are most probably plenty of scrolling and selecting plugins for jQuery you could use. 我不确定你想要实现的功能,但很可能有很多滚动和选择你可以使用的jQuery插件。

One thing I like to do is create a bind function in JavaScript (like what you can find in the Prototype library) specifically for events, so that it passes the "event" object along to the bound function. 我喜欢做的一件事是在JavaScript中创建一个绑定函数(就像在Prototype库中可以找到的那样),专门用于事件,以便它将“event”对象传递给绑定函数。 Now, if you were to do this, you could simply throw in a trace call that will be invoked for every handler that uses it. 现在,如果你要这样做,你可以简单地抛出一个跟踪调用,该调用将为每个使用它的处理程序调用。 And then remove it when it's not needed. 然后在不需要时将其删除。 One place. 一个地方。 Easy. 简单。

However, regardless of how you get the trace statement to be called, you still want to see it. 但是,无论您如何获取要调用的trace语句,您仍然希望看到它。 The best strategy is to have a separate pane or window handing the trace calls. 最佳策略是使用单独的窗格或窗口处理跟踪调用。 Dojo Toolkit has a built-in console that runs in Internet Explorer, and there are other similar things out there. Dojo Toolkit有一个在Internet Explorer中运行的内置控制台,还有其他类似的东西。 The classic way of doing it is to create a new window and document.write to it. 这样做的经典方法是创建一个新窗口和document.write

  • I recommend attaching a date-time to each trace. 我建议在每条迹线上附加日期时间。 Helped me considerably in the past. 过去帮助了我很多。
  • Debugging and alerts usually won't help you, because it interrupts the normal event flow. 调试和警报通常对您没有帮助,因为它会中断正常的事件流。

Matt Berseth has something that may be the kind of thing you're looking for in Debugging ASP.NET AJAX Applications with the Trace Console AjaxControlToolkit Control . Matt Berseth在使用Trace Console AjaxControlToolkit Control调试ASP.NET AJAX应用程序时可能会遇到一些问题。

It's based on the Yahoo YUI logger, YUI 2: Logger . 它基于Yahoo YUI记录器, YUI 2:Logger

My suggestion is, use FireFox together with FireBug and use the built-in Debug/Trace objects. 我的建议是,将FireFox与FireBug一起使用并使用内置的Debug / Trace对象。 They are a charm. 它们是一种魅力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM