After asking a question on Stack Overflow, I have successfully written JavaScript functions for loading partial views dynamically. However, debugging this dynamic loading script has been a challenge for me due to all loaded JavaScript being evaluated by the "eval" function.
Fortunately, I discovered a workaround which involves dynamically creating scripts in the header of the current document. This way, all loaded scripts are visible in the HTML DOM and can be traced using any debugger tool.
var script = document.createElement('script')
script.setAttribute("type","text/javascript")
script.text = "alert('Test!');";
document.getElementsByTagName('head')[0].appendChild(script);
Unfortunately, popular debuggers like IE8 Developer Toolbar, Firebug, and Google Chrome cannot set breakpoints in dynamic scripts as they need to be loaded before the initial page load. This poses a challenge when dealing with dynamic script content or files.
If you have any suggestions on how to effectively debug dynamic scripts, your input would be greatly appreciated!
Update 1 - Testing Source Code
Feel free to utilize the provided xhtml file to debug the value of the variable "someVariable".
<!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>
The results displayed when testing in Firebug should match the images shown below.
Please pay attention to the "dynamicLoadingScript" added after the page load.
However, it does not appear in the script tab of Firebug
Update 2 - Adding Debug Breakpoint
In the above scenarios, adding a "debugger;" statement at certain points in the script triggers a breakpoint in the dynamic loading script. Unfortunately, neither debugger provides any code at the breakpoint making it ineffective.
Thank you for your assistance!