If in script tags of a html page are not "//# sourceurl" for debugging. Can I insert this in chrome developer tools in site code for debugging this javascript code. Or is an other way debug javascript code contain in tags in a loaded html page? I can't change the primary html code.
-
Is the code you want to rename executed with eval() / new Function, or is it an inline <script> tag that the browser parses when the page first loads? DevTools only pays attention to //# sourceURL=… when the directive is present before the code is evaluated.Salt– Salt2025-07-14 13:52:00 +00:00Commented Jul 14 at 13:52
-
I mean a javascript code in a simple <script>alert("hallo")</script> in a html page. Without "//# sourceurl" you can't debug the javascript in dev tools.Gerd– Gerd2025-07-14 13:56:04 +00:00Commented Jul 14 at 13:56
Add a comment
|
1 Answer
//# sourceURL= is read only while the script is being parsed.
Adding it later in DevTools edits the HTML but the code is already executed, so the debugger never sees the directive. Include it inside the <script> before the page loads, then reload:
<script>
alert("hallo");
//# sourceURL=inline-hallo.js
</script>
After reload, inline-hallo.js should appear in Sources and you can debug it.
3 Comments
Gerd
The question is: I have a loaded page and I will debug the javascript in <script> tag. Your answer is no solution. I can't change the primary html code.
Salt
I think I misunderstood. You don’t need //# sourceURL to debug the inline <script>. In DevTools go to Sources -> Page -> (index); the inline code is listed there with line numbers. You can set break-points or add a debugger; line right in that pane and execution will pause without touching the original HTML. Does that approach solve the problem, or are you running into something else?
Gerd
You only see *.js sources in Sources > Page. The problem is javascript in html pages in <script> tag. I think it's only possible debug this with a sourceURL comment. You can edit html in inspect mode in dev tools and you see direct the change on the side. I hope, I can do the same with sourceURL in script tag. But it don't work.