A dynamically-added script is not showing up in the browser's debugger's scripts section.
Explanation:
I need to use and have used
if( someCondition == true ){
$.getScript("myScirpt.js", function() {
alert('Load Complete');
myFunction();
});
}
so that myScript.js can be dynamically loaded on meeting some condition... And myFunction can be called only after getting the whole script loaded...
But browsers are not showing the dynamically loaded myScript.js in their debugger's script section.
Is there another way round so that all of the goals may be achieved which will make one to be able to debug a dynamically-loaded script there in the browser itself?
But fortunately, all modern browsers have a built-in JavaScript debugger. Built-in debuggers can be turned on and off, forcing errors to be reported to the user. With a debugger, you can also set breakpoints (places where code execution can be stopped), and examine variables while the code is executing.
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
You can use //# sourceURL=
and //# sourceMappingURL=
at the end of your script file or script tag.
NOTE: //@ sourceURL
and //@ sourceMappingURL
are deprecated.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With