0

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.

2
  • 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. Commented 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. Commented Jul 14 at 13:56

1 Answer 1

0

//# 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.

Sign up to request clarification or add additional context in comments.

3 Comments

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.
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?
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.