When I open Developer Tools in Google Chrome, I see all kinds of features like Profiles, Timelines, and Audits, but basic functionality like being able to set breakpoints both in js files and within html and javascript code is missing! I tried to use the javascript console, which itself is buggy - for example, once it encounters a JS error, I cannot get out of it unless I refresh the whole page. Can someone help?
17 Answers
Are you talking about code within <script>
tags, or in the HTML tag attributes, like this?
<a href="#" onclick="alert('this is inline JS');return false;">Click</a>
Either way, the debugger
keyword like this will work:
<a href="#" onclick="debugger; alert('this is inline JS');return false;">Click</a>
N.B. Chrome won't pause at debugger
s if the dev tools are not open.
You can also set property breakpoints in JS files and <script>
tags:
- Click the Sources tab
- Click the Show Navigator icon and select the a file
- Double-click the a line number in the left-hand margin. A corresponding row is added to the Breakpoints panel (4).
-
6There is no Scripts tab. I see all other tabs that are in this picture but no Scripts tab !.– aceCommented Mar 1, 2011 at 15:51
-
5I mean being able to set breakpoint within scrip tag in html. I also tried using debugger; keyword but it does not work. Chrome does not breakpoint on that but it works in Firefox. I am looking for breakpoint solution not based on debugger keyword which is inconvenient to put in lot of places in code and then having to delete.– aceCommented Mar 1, 2011 at 15:53
-
2In the drop-down under the Scripts tab I only see JS files listed, but not HTML files that contain the <script> tag that I want to debug. Firebug on FF has a similar drop-down, but also lists HTML files that contain <script> tags. Is this a bug or is there another way to set a breakpoint in inlined JS code? Commented Mar 3, 2011 at 0:11
-
8
-
4When I tried to debug my html embedded js, I got a blank page in my dev area. I had to refresh while looking at the blank source file to get it to populate. Commented Jan 17, 2014 at 19:02
You also can give a name to your script:
<script>
... (your code here)
//# sourceURL=somename.js
</script>
ofcourse replace "somename" by some name ;) and then you will see it in the chrome debugger at "Sources > top > (no domain) > somename.js" as a normal script and you will be able to debug it like other scripts
-
3easiest and perfect way to go. but note for others, remember to remove it before pushing it to production. Commented Feb 14, 2018 at 9:53
-
2Oh my god you have no idea how much this helped me. Oh my god this is brilliant. I've been searching for something like this for ages.– GregRosCommented May 3, 2018 at 19:47
-
I used this (fine) idea, but couldn't see the new file in sources tab, only the html file. Then I executed the code and, at the end, in the console, at the right of whatever message written from the script, you can see a link to the code. The best part is that the link shows the file name you gave to the script (ie, //# sourceURL=somename.js). Commented May 1, 2019 at 8:44
-
4I would give you 1000 upvotes if I could ;) Very useful trick Commented Mar 2, 2020 at 11:53
-
3@ZameerFouzan, why do you need to remove it from production? any real side effect?– oldprideCommented Aug 31, 2020 at 2:28
Use the sources tab, you can set breakpoints in JavaScript there. In the directory tree underneath it (with the up and down arrow in it), you can select the file you want to debug. You can get out of an error by pressing resume on the right-hand side of the same tab.
-
42For some reason I see the list of included js files, but I cannot choose the executing page itself, it doesn't show in the list. Any ideas?– uluCommented Jun 26, 2011 at 11:41
-
69@ulu, I had the same problem. Make sure your type="text/javascript" attribute is set on your <script> element. Commented Dec 13, 2011 at 19:03
-
13except that in HTML5 this attribute is no longer mandatory. I am not changing the source code just to debug ! Commented May 6, 2016 at 9:42
-
11I dont see even after adding type="text/javascript" Commented May 24, 2017 at 16:06
-
9if this does not work, and you have inline javascript code like:
<script> your code </script>
add in a name like:<script> your code //# sourceURL=somename.js </script>
This is explained better in an answer below– KM.Commented Oct 18, 2019 at 13:40
Refresh the page containing the script whilst the developer tools are open on the scripts tab. This will add a (program) entry in the file list which shows the html of the page including the script. From here you can add breakpoints.
-
This didn't work for me. Instead, I clicked on a script error icon in the bottom right to open the program file in the scripts tab. Commented Feb 26, 2013 at 11:26
-
1Thanks! Using Chrome 69, this worked even if I wasn't in the sources tab. Having the developer tools open while the page loaded seems to be the key.– SamCommented Oct 1, 2018 at 1:04
-
This would have been the accepted answer if this was my question. Thank you. Commented Mar 15, 2019 at 8:54
-
Worked in Chrome 83. In my env, the filename is based on the URL of your app. For example, if the URL is example.com/xxx/yyy, the filename will be "yyy". Commented Jun 12, 2020 at 12:14
Another intuitive simple trick to debug especially script inside html returned by ajax, is to temporary put console.log("test") inside the script.
Once you have fired the event, open up the console tab inside the developer tools. you will see the source file link shown up at the right side of the "test" debug print statement. just click on the source (something like VM4xxx) and you can now set the break point.
P.S.: besides, you can consider to put "debugger" statement if you are using chrome, like what is being suggested by @Matt Ball
-
1This is the one I always used to use earlier. Thanks a ton for the reminder. Commented Sep 20, 2022 at 10:57
My situation and what I did to fix it:
I have a javascript file included on an HTML page as follows:
Page Name: test.html
<!DOCTYPE html>
<html>
<head>
<script src="scripts/common.js"></script>
<title>Test debugging JS in Chrome</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<div>
<script type="text/javascript">
document.write("something");
</script>
</div>
</body>
</html>
Now entering the Javascript Debugger in Chrome, I click the Scripts Tab, and drop down the list as shown above. I can clearly see scripts/common.js however I could NOT see the current html page test.html in the drop down, therefore I could not debug the embedded javascript:
<script type="text/javascript">
document.write("something");
</script>
That was perplexing. However, when I removed the obsolete type="text/javascript" from the embedded script:
<script>
document.write("something");
</script>
..and refreshed / reloaded the page, voila, it appeared in the drop down list, and all was well again.
I hope this is helpful to anyone who is having issues debugging embedded javascript on an html page.
-
Good job! Tested in Chrome 20.0.1132.57 on Mac 10.7.4 Submitted bug to Chromium project. Commented Jul 17, 2012 at 23:30
-
Be aware that in HTML 4 and XHTML,
type
is a required attribute. In HTML5, it's optional. Commented Jul 27, 2012 at 14:45 -
2It doesn't have anything to do with type. The script is collected by VM after execution unless inspector is already opened at this point. When you reloaded the page it didn't collect the script.– vsevikCommented Aug 8, 2012 at 12:56
-
I'm my case, missing
<div>
prevented me from seeing the code in the debugger– ceztkoCommented Sep 1, 2018 at 14:49
I was having the same problem too, how to debug JavaScript that is inside <script>
tags. But then I found it under the Sources tab, called "(index)", with parenthesis. Click the line number to set breakpoints.
This is Chrome 71.
-
6
Adding debugger;
on top at my script worked for me.
I know the Q is not about Firefox
but I did not want to add a copy of this question to just answer it myself.
For Firefox you need to add debugger;
to be able to do what @matt-ball suggested for the script
tag.
So on your code, you add debugger
above the line you want to debug and then you can add breakpoints. If you just set the breakpoints on the browser it won't stop.
If this is not the place to add a Firefox answer given that the question is about Chrome. Don't :( minus the answer just let me know where I should post it and I'll happily move the post. :)
If you cannot see the "Scripts" tab, make sure you are launching Chrome with the right arguments. I had this problem when I launched Chrome for debugging server-side JavaScript with the argument --remote-shell-port=9222
. I have no problem if I launch Chrome with no argument.
-
Could you elaborate please? I am having this exact problem - cannot see the scripts tab at all. I downloaded the latest developer build of chrome. Commented Oct 25, 2012 at 16:08
I came across this issue, however my inline function was withing an angularJS view. Therefore on the load i could not access the inline script to add the debug, as only the index.html was available in the sources tab of the debugger.
This meant that when i was opening the particular view with my inline (had no choice on this) it was not accessible.
The onlly way i was able to hit it was to put an erroneous function or call inside the inline JS function.
My solution included :
function doMyInline(data) {
//Throw my undefined error here.
$("select.Sel").debug();
//This is the real onclick i was passing to
angular.element(document.getElementById(data.id)).scope().doblablabla(data.id);
}
This mean when i clicked on my button, i was then prompted in the chrome consolse.
Uncaught TypeError: undefined is not a function
The important thing here was the source of this : VM5658:6
clicking on this allowed me to step through the inline and hold the break point there for later..
Extremely convoluted way of reaching it.. But it worked and might prove useful for when dealing with Single page apps which dynamically load your views.
The VM[n]
has no significant value, and the n
on equates to the script ID. This info can be found here : Chrome "[VM]"
Using Visual Studio (2012) I had the same issue and switching to IIS Express solved the problem!
The script
tag's type
attribute did not factor into it.
For some reason the Visual Studio Development Server does not provide everything Chrome needs to enable the breakpoints.
This is an extension of Rian Schmits' answer above. In my case, I had HTML code embedded in my JavaScript code and I couldn't see anything other than the HTML code. Maybe Chrome Debugging has changed over the years but right-clicking the Sources/Sources tab presented me with Add folder to workspace. I was able to add my entire project, which gave me access to all of my JavaScripts. You can find more detail in this link. I hope this helps somebody.
You have to add type="text/javascript"
to the script blocks for them to be picked up as script. For example:
<script type="text/javascript"> ...your code here... </script>
for someone like me: just want to
- (add breakpoint to) debug normal js code
<script> ... </script>
inside/embedded html
Steps:
None of the answers above worked for my case: wanting to add a breakpoint to an already-running script, that had been inserted into the page as a <script>
element, from a browser extension I don't control (and don't want to have to download and modify the code of just to do some quick exploration/checking).
Problems:
- The "Sources" panel does not show
<script>
tags added after the page had loaded. (and refreshing the page to have it show up under a(program)
entry in the file list, as per this answer, did not seem to work) - Adding a
breakpoint;
line to the script does not work -- nicely anyway -- because I would need to re-execute the entire script tag to do that, which I don't want to do (I merely want to add a breakpoint to the already executed/initialized code-block). - Adding
type="text/javascript"
after-the-fact did not seem to change anything. (and as mentioned, I don't want to re-execute the script) - Adding
//# sourceURL=somename.js
after-the-fact did not seem to change anything. (and as mentioned, I don't want to re-execute the script)
My solution: Select an element in the Elements panel that I know the relevant script added an event listener to. Then open the "Elements->Event listeners" panel, look through the listeners created from VM.....
scripts, and click into those VM.....
scripts till I find one starting with the code I know that script starts with. That then opens the Sources
page in dev-tools, with the correct script selected -- along with the ability to add breakpoints and such!
- Right click anywhere in the page with the imbedded script and click "Inspect".
- Switch to "Source" view.
- If the script is not under "Contents Scripts" in the left panel, press Ctrl-P or click on "Open File" link and select the HTML page you are interested in.
- Proceed with debugging.
.php
file and Chrome just couldn't show the source file inSource
Tab of Chrome devtool. I tried Firefox and everything works as expected.