Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically control breakpoints in Javascript?

Is it possible, in any browser, using any plugin, to enable or disable breakpoints in your code programmatically?

I already know about setting conditional breakpoints, but I'm really interested in setting them via code.

like image 620
nickf Avatar asked Mar 11 '11 10:03

nickf


People also ask

Can you set breakpoints in JavaScript?

In the debugger window, you can set breakpoints in the JavaScript code. At each breakpoint, JavaScript will stop executing, and let you examine JavaScript values. After examining values, you can resume the execution of code (typically with a play button).

How do you hit a breakpoint in JavaScript?

To enable the new multi-target debugger in VS 16.7 or newer please go to Tools -> Options -> Debugging -> General -> and check the option: “Enable using the multi-target JavaScript debugger for debugging JavaScript in ASP.NET projects (requires debugging restart)”

Why do we use the word debugger in JavaScript?

The debugger keyword is used in the code to force stop the execution of the code at a breaking point and calls the debugging function. The debugger function is executed if any debugging is needed at all else no action is performed.

What is a debugger statement?

The debugger statement invokes any available debugging functionality, such as setting a breakpoint. If no debugging functionality is available, this statement has no effect.


2 Answers

First you could add a call to a function like __checkDebug(); which will check for a global (or semi-global) variable and when said variable is true, call debugger.

   function __checkDebug() {    if (debugme) debugger; }  

all of your functions you're concerned about debugging would be like so:

   function foo() {    __checkDebug();     //.... whatever foo was gonna do. }  

You can then take it a little further and dynamically decorate functions while the code is being executed like so:

 Function.prototype.debug = function(){       var fn = this;     return function(){             if (debugme) debugger;         return fn.apply(this, arguments);         };  };   foo = foo.debug();    

now any time foo is called it will call debugger if the debugme variable is truthy.

Another option would be to build a javascript build system that injects the call after every function declaration - this requires a syntax parser but if you're only looking to modify functions a simple tokenizer for that use case is pretty easy to write - but I'll leave that up to you.

like image 170
Marcus Pope Avatar answered Sep 18 '22 13:09

Marcus Pope


You can use debugger; in code to make breakpoint for firebug. For example:

alert('1'); debugger; alert('2'); 

And firebug automatically stops on this keyword.

like image 32
CoolEsh Avatar answered Sep 18 '22 13:09

CoolEsh