How to set a JavaScript breakpoint from code in Chrome? - javascript

I want to force the Chrome debugger to break on a line via code, or else using some sort of comment tag such as something like console.break().

You can use debugger; within your code. If the developer console is open, execution will break. It works in firebug as well.

You can also use debug(function), to break when function is called.
Command Line API Reference: debug

Set up a button click listener and call the debugger;
Example
$("#myBtn").click(function() {
debugger;
});
Demo
http://jsfiddle.net/hBCH5/
Resources on debugging in JavaScript
http://www.laurencegellert.com/2012/05/the-three-ways-of-setting-breakpoints-in-javascript/
http://berzniz.com/post/78260747646/5-javascript-debugging-tips-youll-start-using-today

As other have already said, debugger; is the way to go.
I wrote a small script that you can use from the command line in a browser to set and remove breakpoint right before function call:
http://andrijac.github.io/blog/2014/01/31/javascript-breakpoint/

debugger is a reserved keyword by EcmaScript and given optional semantics since ES5
As a result, it can be used not only in Chrome, but also Firefox and Node.js via node debug myscript.js.
The standard says:
Syntax
DebuggerStatement :
debugger ;
Semantics
Evaluating the DebuggerStatement production may allow an implementation to cause a breakpoint when run under a debugger. If a debugger is not present or active this statement has no observable effect.
The production DebuggerStatement : debugger ; is evaluated as follows:
If an implementation defined debugging facility is available and enabled, then
Perform an implementation defined debugging action.
Let result be an implementation defined Completion value.
Else
Let result be (normal, empty, empty).
Return result.
No changes in ES6.

On the "Scripts" tab, go to where your code is. At the left of the line number, click. This will set a breakpoint.
Screenshot:
You will then be able to track your breakpoints within the right tab (as shown in the screenshot).

There are many ways to debug JavaScript code. Following two approaches are widely used to debug JavaScript via code
Using console.log() to print out the values in the browser
console. (This will help you understand the values at certain points
of your code)
Debugger keyword. Add debugger; to the locations you want to
debug, and open the browser's developer console and navigate to the
sources tab.
For more tools and ways in which you debug JavaScript Code, are given in this link by W3School.

It is possible and there are many reasons you might want to do this. For example debugging a javascript infinite loop close to the start of the page loading, that stops the chrome developer toolset (or firebug) from loading correctly.
See section 2 of
http://www.laurencegellert.com/2012/05/the-three-ways-of-setting-breakpoints-in-javascript/
or just add a line containing the word debugger to your code at the required test point.

Breakpoint :-
breakpoint will stop executing, and let you examine JavaScript values.
After examining values, you can resume the execution of code (typically with a play button).
Debugger :-
The debugger; stops the execution of JavaScript, and callsthe debugging function.
The debugger statement suspends execution, but it does not close any files or clear any variables.
Example:-
function checkBuggyStuff() {
debugger; // do buggy stuff to examine.
};

You can set debug(functionName) to debug functions as well.
https://developers.google.com/web/tools/chrome-devtools/javascript/breakpoints#function

I wouldn't recommend debugger; if you just want to kill and stop the javascript code, since debugger; will just temporally freeze your javascript code and not stop it permanently.
If you want to properly kill and stop javascript code at your command use the following:
throw new Error("This error message appears because I placed it");

This gist Git pre-commit hook to remove stray debugger statements from your merb project
maybe useful if want to remove debugger breakpoints while commit

Related

Debug a js file in Chrome DevTools if any line of it is executed

I'm trying to debug input events in my code.
I'm interested in one file full of fuctions declarations. I know that if I put one debug point in every first single line of all the functions I can know if one of them is executed.
Is there another way to do that? I mean I need the debug mode starts if any line of this .js file is executed.
Thanks a lot
Debugger Statement
You can use the debugger; statement. You leave it in the code, and it will cause the debugger to execute.
What I would do would be to create a custom function e.g. debugMe(); where inside you would call console.log and also debugger;
Yes, there is a way. You can add debugger statement at the start of each function. Read more about it over here.

What are the rights ways of debugging javascript?

I made a function called test() in javascript file.Placed a simple alert into it.
In html file, called the method on click of a button. But,it was not being invoked.
Problem was in the 11th function, nowhere related to mine !!!! But, how can a person making his first javascript function suppose to find that out ???
I am looking for best ways to debug javascript.
You can debug javascript using many modern browsers. See this question for details on how to debug in Google Chrome:
How do you launch the JavaScript debugger in Google Chrome?
Furthermore, you shouldn't use alert() for debugging as this can give different results to a production version due to alert() causing a pause in the script.
It is best practice to use console.log() and view the output in the browsers Console.
You can also put debugger in your javascript code to force a breakpoint. However I prefer not to use this as forgetting to remove this before deployment will cause your script to pause, which can be quite embarrassing!
You should use the debug console provided by the browser.
Chrome has it inbuilt, press CTRL + SHIFT + j. In Firefox, install Firebug plugin.
In your code, add alert() to show flow and get values of variables.
Also, use console.log() which will only output to the debug console.
Depending on your browser choice there are debugging options - I tend to use Firefox, so Firebug in my case. There is a question that list options for other browsers - What is console.log and how do I use it?
Unless the project you're working on has already adopted a mechanism for debugging, console.log() tends to be a simple and useful option when tracking down a problem.
Whilst debugging you could take the approach to log out a line when entering a function, like so:
var myFunc = function(el) {
console.log('Inside myFunc');
// Existing code
};
This will enable you to see which functions have been called and give you a rough idea of the order of execution.
You can also use console.log() to show the contents of variables - console.log(el);
Be mindful to remove/disable console.log() calls once you're done as it will likely cause some issues in production.
To answer your question within question,
how can a person making his first javascript function suppose to find that out ???
Well, when something is wrong in JavaScript, for example, you made a syntax error - the script will stop working from there. However, this won't stop HTML from rendering on, so it might look as if everything is correct (especially if your JS is not changing the look of the page) but all the functionality of JS will be dead.
That's why we use the debug tools (listed in the other answers here) to see what's wrong, and in cases like this, it's very easy to notice which function has errors and is causing the whole script to break. This would probably have save a few minutes to your seniors as well.
The best approach would be to test frequently so that whenever you run into errors, you can fix them right away.

How to access JavaScript execution trace at runtime in Firefox?

I want to know how to access JavaScript execution trace at runtime. I saw Firebug can do something like this:
Refer to the image above, all the line numbers executed are highlighted in green. They are achieved at runtime. I guess there must be some way to access those info from the JavaScript engine used by the browser.
Say now I want to build a firebug plugin to access those info and examine all the variables in each executed line at the runtime, how should I start?
Obviously you asked the same question in the Firebug forum.
To duplicate Honza's answer:
Firebug is currently using JSD (jsdIDebuggerService) to figure out,
which line is executable. However, the plan is to switch to JSD2 (work
in progress) https://wiki.mozilla.org/Debugger
You should also base your extension on JSD2
Look for getLineOffsets(line) and getOffsetLine(offset) in the
Debugger document. I didn't test it, but I think that if getLineOffset
returns null, the line is not executable.
Sebastian

find from what file and what line javascript function was invoked

After using functionality from this question for debugging I am wondering is there a way to get the file name from which the function was invoked and may be the line.
May be I am asking for too much, but I know that in some of the languages it is possible.
If this is not possible can anyone mention why his functionality was not implemented?
I will try to rephrase the question, because I think I didn't make myself clear.
I have file.js in which on the 17-th line there is a declaration of the function:
...
function main()
{
Hello();
}
I have another file test.js, where I define function hello
function Hello()
{
...
which tells me the name of the file and a line in which the function which evoked it was defined
}
So for example if I will call
main(), it will tell me file.js, 17 line
It has nothing to do with firebug
If all your doing is debugging, hy not just use the debugger built into modern browsers?
debugger
That line is all you need. You can examine the callstack, inspect variable values, and even run code in the current scope.
Such functionality makes your request kind of unnecessary.
You don't mention what browser you are using.
In Chrome what I would suggest is inserting a breakpoint on the first line of the function. Then reload the page (or otherwise trigger the function call). When execution pauses at the breakpoint, check the Call Stack section in Chrome's Developer tools - it will give you a stack back-trace of the flow of execution.
I'm sure Firebug offers something similar if not identical, it's just been a while since I used it.
Cheers

How to debug Javascript error?

How to Debug java Script Error Using Firebug?
Duplicate
How can I set breakpoints in an external JS script in Firebug
Debug using FireBug.
Just check the line on which the error is occuring, then, just before that line, write a "debugger" call.
debugger; //Will invoke FireBug's debugger.
var err = abcs; //Line containing error
To debug an error in firebug :
1- select inspect tab from menu
2- Set break point in the line that
causes error
3- Refrsh the page
4- use F10 to step by step debug and
F5 to end debgging
It's like debgging of visual studio
Use the console.log(yourObject) function to output anything to the firebug console. It is just like running a var_dump and you can view all your objects and their contents. This is very helpful if you want to check on the contents of a particular variable or even a particular DOM object.
Instead of using cheap alerts - the console.log() function is cleaner and you can see all the outputs neatly in your console pane.
Note however you need to remove all references to the console.log function when you deploy your website as it would not run in IE.
you can put breakpoints in the code, and wait for the execution to hit them. Then you can walk in the code, and use watches to know the values of variables.
You can use Firebug, and for debugging in chrome, you can use firebug lite
Why Firebug , try Visual studio , it has a rich debugging features ;)

Categories