[英]Telling Firebug to Break as Soon as any Javascript is Executed
I've inherited a pile of code that's doing unexpected things when I click on some <button>
elements. 当我点击一些
<button>
元素时,我继承了一堆做出意想不到的事情的代码。
The page has a lot of Javascript, and it's unclear what, if any, events have been setup on the button or its parent elements. 该页面有很多Javascript,并且不清楚在按钮或其父元素上设置了什么(如果有的话)事件。 I'm trying to find where execution starts so I can debug what's going on.
我正在尝试找到执行开始的地方,以便我可以调试正在进行的操作。
Is there a way to have Firebug (or any Javascript debugger) break whenever any javascript code is executed? 有没有办法让Firebug(或任何Javascript调试器)在执行任何 javascript代码时中断? Maybe someway to setup a break point on each line programmatically?
也许某种程度上以编程方式在每一行上设置一个断点? Or is there some other way to find out which event the button is responding to (page users boh Prototype, jQuery, and some custom Javascript. I know.)
或者是否有其他方法可以找出按钮响应的事件(页面用户boh Prototype,jQuery和一些自定义Javascript。我知道。)
Just press Pause button on the firebug panel (on the left-top corner of it, two yellow vertical lines, when Script tab selected) - and it will stops at first JS string. 只需按下firebug面板上的Pause按钮(在它的左上角,两条黄色垂直线,当选择Script选项卡时) - 它将在第一个JS字符串处停止。
But it will stop JS executing really at ANY event - so if you have a button which catches mouse move and mouse click - i will not be able to stop on mouse click, since script will be stopped at mouse move each time you try to move mouse over this button. 但它会阻止JS在任何事件中执行 - 所以如果你有一个抓住鼠标移动和鼠标点击的按钮 - 我将无法在鼠标点击停止,因为每次你试图移动鼠标移动时脚本都会停止将鼠标悬停在此按钮上 I suggest you just to insert alert()'s in the suspicious places - after some iterations you will found exactly string you need (where error occurs).
我建议你只是在可疑的地方插入alert() - 经过一些迭代后你会发现你需要的确切字符串(发生错误的地方)。
Close the Internet browser and open it again. 关闭Internet浏览器并再次打开它。 "Break on next" will start working again.
“打破下一步”将再次开始工作。 Otherwise Firebug simply ignores this option after a while, and doesn´t break but run javascripts for all events without warning you.
否则Firebug会在一段时间后忽略此选项,并且不会中断,但会在没有警告的情况下为所有事件运行javascripts。
I found a bookmarklet that will list all bound events in your page . 我发现了一个书签,它将列出页面中的所有绑定事件 。
This kind be useful if you don't know what events are bound by third-party code. 如果您不知道哪些事件受第三方代码约束,则此类很有用。
如果您想查看页面上设置的事件,您也可以尝试使用Eventbug 。
If this is a generic debugging problem you can use the Web Inspector in Safari / Chrome. 如果这是一般调试问题,您可以使用Safari / Chrome中的Web Inspector。 An added bonus is that you can throw the following statement into your JavaScript code to pull up the debugger:
另外一个好处是,您可以将以下语句放入JavaScript代码中以调出调试器:
...
debugger; // Will trigger the debugger
...
Otherwise you can set breakpoints, conditional breakpoints, pause on the next execution, and all the usual tricks. 否则,您可以设置断点,条件断点,下次执行时暂停以及所有常用技巧。 Some nice videos are available here .
这里有一些不错的视频。
In response to the alert
suggestion before. 响应之前的
alert
建议。 I would seriously recommend using console.log
or console.dir
if you have access to a debugger. 如果您有权访问调试器,我会认真推荐使用
console.log
或console.dir
。 You will get more information without blocking your script's execution. 您将获得更多信息,而不会阻止脚本的执行。 Also, don't forget that that
console.log
can take multiple parameters so you can nicely print out data, you don't have to constrain yourself to a single string like alert
. 另外,不要忘记
console.log
可以使用多个参数,这样您就可以很好地打印出数据,您不必将自己限制在像alert
这样的单个字符串中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.