Using Google Chrome to debug and edit javascript embedded in HTML page

JavascriptDebuggingGoogle Chrome

Javascript Problem Overview


Chrome developer tools allows you to edit javascript in the browser if the javascript is in a .js file. However, it does not seem to allow me to edit javascript that is embedded in an HTML page. ie:

<script type="text/javascript> 
// code here that I want to debug/edit
</script> 

This is a big problem for me as I have quite a bit of embedded javascript in a certain page.

Similar to this question: https://stackoverflow.com/questions/4764245/edit-javascript-blocks-of-web-page-live but this is about firefox, not chrome.

How can I edit javascript embedded in an HTML page using Google Chrome Developer Tools?

Javascript Solutions


Solution 1 - Javascript

Actually chrome allows to do that, choose HTML files in Sources tab in Developer tools window. You will see HTML instead of javascript and simply add breakpoints in the <script> tags. Also you can add debugger; command to script what you want to debug. For example:

<script>
 // some code
 debugger; // This is your breakpoint
 // other code you will able to debugg
</script>

Don't forget to remove debugger;'s when you want to release your website.

Solution 2 - Javascript

I had a difficult time locating the file that had my inline/embedded javascript. For others having the same problem, this may or may not be helpful...

Using Chrome 21.0.1180.89 m for Windows

enter image description here

All files are shown after clicking that very discreetly placed button. See:

enter image description here

Now you may begin debugging...

enter image description here

Solution 3 - Javascript

None of these answers have worked for me.

What works for me is if I have my javascript on the page already loaded, I can copy that javascript, edit it, then paste it in the console and it will redefine any functions or whatever that I need to be redefined.

for instance, if the page has:

<script>
var foo = function() { console.log("Hi"); }
</script>

I can take the content between the script, edit it, then enter it into the debugger like:

foo = function() { console.log("DO SOMETHING DIFFERENT"); }

and it will work for me.

Or if you have like,

function foo() {
    doAThing();
}

You can just enter

function foo() {
    doSomethingElse();
}

and foo will be redefined.

Probably not the best workaround, but it works.

Solution 4 - Javascript

Go to the Elements tab, find your script, right click on the part you need and choose "Edit as HTML".

If Edit as HTML doesn't appear, double click the node and it should become highlighted and editable.

Solution 5 - Javascript

Solution described here: https://greatrexpectations.com/2014/01/22/chrome-dev-tools-inline-dynamic-javascript

Add the 'sourceURL' directive in your inline/embedded script like this:

<script type="text/javascript">
...script here...
//# sourceURL=helperJavaScript.js
</script>

Then this script will appear in the page Sources and you can debug and edit it similarly to js loaded from a URL source enter image description here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMark RobinsonView Question on Stackoverflow
Solution 1 - JavascriptantyratView Answer on Stackoverflow
Solution 2 - JavascriptTheoView Answer on Stackoverflow
Solution 3 - JavascriptbyronalticeView Answer on Stackoverflow
Solution 4 - Javascriptbrimble2010View Answer on Stackoverflow
Solution 5 - JavascriptIoannis K. MoutsatsosView Answer on Stackoverflow