Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript watch expression based breakpoints with Firebug?

Working through some event routing right now and there's a lot of debugging steps.

I know about using "debugger" in the javascript and putting that after a conditional, and that is useful. I also know about right clicking a break point to add a test expression which is even better. However... I have no idea where this thing is going to take me and I am starting to wear out my function keys. Is there any way to add a breakpoint to a watch expression?

Basically the idea is this, within the enclosure scope, I want to check for a variable called "this.id". If this.id is the value I want, I enter the debugger.

Any ideas?

Thanks

Wanted to add that Didier's answer below solved my problem as they outlined in the article for decorating "Function". This will most likely be the path of least resistance for searching all functions for the value I want.

Function.prototype.debug = function(){   
   var fn = this; 
   return function(){     
       if (debugme) debugger; 
       return fn.apply(this, arguments);     
   }; 
};
like image 281
Shane Avatar asked Nov 09 '11 21:11

Shane


People also ask

How to debug JavaScript code in Firebug?

In Script tab of FireBug, You need to select _display file. It has the code written in javascript section of jsFiddle. Just put breakpoint and debug easily.

How do I find the breakpoint on a line of code?

To the left of the code you can see the line number of this particular line of code, which is 32. Click on 32. DevTools puts a blue icon on top of 32. This means that there is a line-of-code breakpoint on this line.

Why pause your code with breakpoints?

See Pause Your Code With Breakpoints to learn when and how to use each type. One common cause of bugs is when a script executes in the wrong order. Stepping through your code enables you to walk through your code's execution, one line at a time, and figure out exactly where it's executing in a different order than you expected.

When to use each breakpoint type?

Overview of when to use each breakpoint type # Breakpoint Type Use This When You Want To Pause... Line-of-code On an exact region of code. Conditional line-of-code On an exact region of code, but only whe ... DOM On the code that changes or removes a sp ... XHR When an XHR URL contains a string patter ... 3 more rows ...


2 Answers

To programmatically break on javascript code, you can use the following statement:

debugger;

This works in Firebug, Chrome's console and IE.

So following your question, you could do something like:

if (this.id === "myId")
    debugger;

The following article is pretty useful.

like image 177
Didier Ghys Avatar answered Sep 30 '22 17:09

Didier Ghys


If you are referring to "conditional breakpoints", that is to say a breakpoint that only pauses execution when a statement is true, you can do this by right-clicking on a line of script, and selecting "Edit Breakpoint Condition...", then adding the statement you want to trigger the breakpoint, e.g., this.id === "foo";

like image 40
d60402 Avatar answered Sep 30 '22 17:09

d60402