可以通过 WebKit、 FireBug 或 IE8开发工具等调试器调试动态加载 JavaScript 吗?

我最近的问题开始,我已经创建了一些用于动态加载部分视图的 JavaScript 函数。但是我不能调试任何动态加载 JavaScript。因为所有加载的 JavaScript 都将由“ eval”函数计算。

我发现了一种创建新 JavaScript 的方法,即使用下面的脚本将脚本动态创建到当前文档的标题中。所有加载的脚本都将显示在 HTMLDOM 中(您可以使用任何调试器来查找它)。

var script = document.createElement('script')
script.setAttribute("type","text/javascript")
script.text = "alert('Test!');";


document.getElementsByTagName('head')[0].appendChild(script);

顺便说一下,大多数调试器(IE8DeveloperToolbar、 Firebug 和 GoogleChrome)不能在任何动态脚本中设置断点。因为必须在加载页之后第一次加载可调试脚本。

当使用动态脚本内容或动态文件时,您有调试的想法吗?

更新1-添加用于测试的源代码

您可以使用下面的 xhtml 文件来尝试调试一些变量值。

<!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>

从答案,我只是测试它在 FireBug。结果应该显示如下图像。

alt text

请查看页面加载后添加的“ DynamicLoadingScript”。

alt text

但是在 FireBug 的 script 选项卡中没有找到

更新2-在动态加载脚本中创建调试断点

alt text

alt text

上述两个图像都显示在脚本的某一行中插入“ debug;”语句可以在动态加载脚本中触发断点。但是,两个调试器在断点处都不显示任何代码。因此,这是没有用的

谢谢

50269 次浏览

In Firebug, you should be able to see that script after the page is loaded and the script is injected. When you do, you can set a breakpoint in the appropriate place, and it'll be preserved when you refresh the page.

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.

I think you might need to give the eval'd code a "name" like this:

http://blog.getfirebug.com/2009/08/11/give-your-eval-a-name-with-sourceurl/

If you do, I think it's likely the debugger approach from "update 2" should work then.

Dynamicly loaded Javascript still has to be parsed by the browser this is where WebKit, or FireBug debugger is sat so it's subject to the debugger no matter what, i think this is the same for the developer tools in IE8,

So your code is subject is to the debugger so where your getting a problem will not be in that file or text if it does not error

The other thing is script.text = "alert('Test!');"; is not valid so it wont work in all browsers what you want is script.innerHTML = "alert('Test!');";

even though its innerHTML it means code inside the HTML Tags not the HTML inside just the most use people use it for this so it gets explained wrong

EDITED FOR UPDATE TWO

And on Second update using Chrome i did this

go to about:blank Open the console up and past in

var script = document.createElement('script')
script.setAttribute("type","text/javascript")
script.innerHTML = "alert('Test!');debugger;";


document.getElementsByTagName('head')[0].appendChild(script);

then it will break and open the script tab with about:blank shown (nothing to see)

Then on the right hand side show the call stack list, then click on the second (anonymous function) and it will show you.

So on your file you will have a (anonymous function) that is the code your running and you will see the break point in there. so you know your in the right one.

Using Google Chrome (or Safari) Developers Tool, you can run JavaScript line by line.

Developer Tool > Scripts > Choose which script you want to debug > pause sign on the right side Or set breakpoints by click the line number

Using Chrome(12.0.742.112) with the code you provided plus a debugger statement like this

    script.text =   "debugger;var someVariable = 0;\n" +
"someVariable = window.outerWidth;\n" +
"alert(someVariable);";

chrome_debugger

works for me.

I need to modify some JavaScript (limiting scope of all jQuery selector to current partial >view div) before execute it.

May its more tangible if you bind the selector change to an event on your partial view instead of creating script elements in the html body ( doesnt feel right ).

You could do something like this

   (function(j)(
var limiting_selector = '';
j(".partial_views").bind('focusin over',function(e){
limiting_selector = j(this).attr('someattr') // or j(this).data('limiting-selector')
}).bind('focusout out',function(e){
limiting_selector = '';
});
// And then go on with
// j(limiting_selector+' .someclass')
))(jQuery)

This code would always add a limiting selector to all jQuery select operations done while the mouse is in a certain element given the HTML isnt to messed up.

(Still seems hackerish, may be someone has a better solution)

cheers

I'm using google chrome for that purpose.

In chrome at scripts tab you can enable 'pause on all exceptions'

enter image description here

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.

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

UPDATE: the syntax for sourceUrl has been changed (@ is replaced by #) to avoid errors on unsupported browsers (read: IE). Details

One option I like to use it adding a console.log('') statement in my code. Once this statement appears in the console a line number is associated with it. You can click that number to go to the location in the source and set a breakpoint. The drawback to this approach is that breakpoints are not preserved across page reloads and you have to run through the code before you can add a debugger.

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.

  1. Open Console of chrome and write the name of the method and hit enter.
    In my case, it is GetAdvancedSearchConditonRowNew
    If the JS method has loaded then it will show the definition of the method.

enter image description here


  1. Click on the definition of the method and the whole JS file will be opened for debugging :)

enter image description here


Method 2:

As an example, I'm loading JS file when I click on a button using ajaxcall.

  1. Open network tab in google chrome dev tools
  2. Click on a control (ex. button) which loads some javascript file and calls some javascript function.
  3. observe network tab and look for that JS function (in my case it is RetrieveAllTags?_=1451974716935)
  4. Hover over its initiater and you'll find your dynamically loaded JS file(with prefix VM*).

debug dynamic loading JavaScript in chrome -1


  1. Click on that VM* file to open.
  2. Put debugger whereever you want in that file :D

debug dynamic loading JavaScript in chrome -1