I add a script dynamically as shown in the code below:
$.getScript("myScript.js");
The browser is not showing myScript.js in the debugger file list.
How can I show scripts added dynamically in the browser debugger?
You can give your dynamically loaded script a name so that it shows in the Chrome/Firefox JavaScript debugger. To do this you place a comment at the end of the script:
//# sourceURL=filename.js
This file will then show in the "Sources" tab as filename.js. In my experience you can use 's in the name but I get odd behaviour if using /'s.
Since if a domain it is not specified filename.js will appear in a folder called "(no domain)" it is convenient to specify a domain for improving the debugging experience, as an example to see a "custom" folder it is possible to use:
//# sourceURL=browsertools://custom/filename.js
A complete example follows:
window.helloWorld = function helloWorld()
{
console.log('Hello World!');
}
//# sourceURL=browsertools://custom/helloWorld.js
For more information see: Breakpoints in Dynamic JavaScript deprecation of //@sourceurl
I tried using the "//# sourceURL=filename.js" that was suggested as a workaround by the OP, but it still wasn't showing up for me in the Sources panel unless it already existed in my tabs from a previous time when it produced an exception.
Coding a "debugger;" line forced it to break at that location. Then once it was in my tabs in the Sources panel, I could set breakpoints like normal and remove the "debugger;" line.
debugger;, and DevTools had to be open while the script was loading.//# sourceURL=browsertools://yourdomaingoeshere.com/action-openuwws.js//# sourceURL=filename.js so that the js would show in the source tree, but I like using debugger; because I can put it anywhere, and I can place it in the source code before loading app. Once in app, it acts as the initial breakpoint, then I can add more via the DevTools or watch variables etc as per normal.You can use //# sourceURL= and //# sourceMappingURL= at the end of your script file or script tag.
NOTE: //@ sourceURL and //@ sourceMappingURL are deprecated.
sourceMappingURL is to debug code that has been minified and understand the Mapping. But personally, just debug not on production... but on source code.Notice that the source file appearing in the sources tab this way will appear in the (no domain) group and, in case you want to debug it, you will need to add a debugger; line in your code, make that line be executed (usually at the start of the execution of your source file) and then add your breakpoints wherever you want.
In case you are debugging production stages, where you will probably have no debugger; lines in your code, you can make this happen by doing a local map with CharlesProxy to your "fresh copy of the source file with the debbuger line inserted".
debugger;to auto stop in the dynamic loaded script, see javascript.info/debugging-chrome