Is possible to debug dynamic loading JavaScript by some debugger like WebKit, FireBug or IE8 Developer Tool?
From my recent question, I already created some JavaScript function for dynamic loading partial view. So, I can't debug any dynamic loading JavaScript. Because all of loaded JavaScript will be evaluated by "eval" function.
However, I found some way to create new JavaScript by using the following script to dynamically create script into header of current document. All loaded scripts will be displayed in HTML DOM (that you can use any debugger to find it).
var script = document.createElement('script')
script.setAttribute("type","text/javascript")
script.text = "alert('Test!');";
document.getElementsByTagName('head')[0].appendChild(script);
By the way, most debugger (IE8 Developer Toolbar, Firebug and Google Chrome) can’t set breakpoint in any dynamic script. Because debuggable script must be loaded at the first time after page is loaded.
Do you have idea for debugging in dynamic script content or file?
Update 1 - Add sourcecode for testing
You can use the following xhtml file for trying to debug someVariable value.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Dynamic Loading Script Testing</title>
<script type="text/javascript">
function page_load()
{
var script = document.createElement('script')
script.setAttribute("id", "dynamicLoadingScript");
script.setAttribute("type","text/javascript");
script.text = "var someVariable = 0;\n" +
"someVariable = window.outerWidth;\n" +
"alert(someVariable);";
document.getElementsByTagName('head')[0].appendChild(script);
}
</script>
</head>
<body onload="page_load();">
</body>
</html>
From answer, I just test it in FireBug. The result should be displayed like below images.
alt text http://d7cgnq.bay.livefilestore.com/y1pJGV27qEARXzZhsMMhPD5z0OxAtnkQK8eKeEXMAxuHjsuWeAq1aGl7sTISRNqVv3lw8ghyxE_GorlwSJDvTql0m0ugSGXPaY-/undebugable-firebug-html.PNG
Please look at "dynamicLoadingScript" script that is added after page load.
alt text http://d7cgnq.bay.livefilestore.com/y1pz_RwqjgeVqe2mAlUZha0PalpPdP0X5AiwJcQhYahERlnbPDdZpb-_d1e96Q-14raSDMJOBsdcV9jsi4B5yCUDqm41Wc3h0yk/undebugable-firebug-script.PNG
But it does not found in script tab of FireBug
Update 2 - Create Debug Breakpoint in dynamic loading script
alt text http://d7cgnq.bay.livefilestore.com/y1peKdIMOztXss-pc8PlJEmaGov_0ShYnLsuDdK5OLETbCG0UpkX6Te29w0YW2pNKaD_0ANAiizh2GdGPAAp_1q0JWAETo4Qoiw/Debugger-VS2010.png
alt text http://d7cgnq.bay.livefilestore.com/y1pUeHaHoTXj0WBl74fnTrXwdr_y7s7h8jNKfslSONgshzZBpUddGHNBWA9cXRabo3HAl3QazeozrCABo7pNVQc1Acl-UXXbHFE/Debugger-GoogleChrome.png
Both of the above images show inserting "debugger;" statement in some line of script can fire breakpoint in dynamic loading script. However, both debuggers do not show any code at breakpoint. Therefore, it is useless for doing this.
Thanks,
It would also be possible to use chrome for the same. Chrome has a feature where you can specify a parser attribute and make the piece of dynamic JS appear as a file which can then be browsed to and break points set.
the attribute that needs to be set is
//# sourceURL=dynamicScript.js
where dynamicScript.js is the name of the file that should show up in the script file browser.
More information here
Paul Irish also talks about it briefly in his excellent talk on Tooling & The Webapp Development Stack
Try adding a "debugger;" statement in the javascript you're adding dynamically. This should cause it to halt at that line regardless of breakpoint settings.
Yes, It is (now) possible to debug dynamically loaded JavaScript using Google Chrome!
No need to add extra debugger;
or any other attribute for dynamically loaded JS file. Just follow the below steps to debug:
Method 1:
My tech lead just showed a super-easy way to debug dynamically loaded Javascript methods.
- Open Console of chrome and write the name of the method and hit enter.
In my case, it isGetAdvancedSearchConditonRowNew
If the JS method has loaded then it will show the definition of the method.
- Click on the definition of the method and the whole JS file will be opened for debugging :)
Method 2:
As an example, I'm loading JS file when I click on a button using ajax
call.
- Open
network
tab in google chrome dev tools - Click on a control (ex. button) which loads some javascript file and calls some javascript function.
- observe network tab and look for that JS function (in my case it is
RetrieveAllTags?_=1451974716935
) - Hover over its
initiater
and you'll find your dynamically loaded JS file(with prefixVM*
).
- Click on that
VM*
file to open. - Put debugger whereever you want in that file :D
I'm using google chrome for that purpose.
In chrome at scripts tab you can enable 'pause on all exceptions'
And then put somewhere in your code line try{throw ''} catch(e){}
. Chrome will stop execution when it reaches this line.
EDIT: modified image so it would be clearer what I'm talking about.