简体   繁体   English

如何在浏览器的调试器本身中调试动态加载的JavaScript(使用jQuery)?

[英]How to debug dynamically loaded JavaScript (with jQuery) in the browser's debugger itself?

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... 这样myScript.js可以在满足某些条件时动态加载......并且只有在加载完整个脚本后才能调用myFunction ...

But browsers are not showing the dynamically loaded myScript.js in their debugger's script section. 但是浏览器没有在调试器的脚本部分显示动态加载的myScript.js。

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? 是否有另一种方法可以实现所有目标,这将使人们能够在浏览器本身中调试动态加载的脚本?

You can give your dynamically loaded script a name so that it shows in the Chrome/Firefox JavaScript debugger. 您可以为动态加载的脚本指定一个名称,以便在Chrome / Firefox JavaScript调试器中显示。 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 . 然后,此文件将在“Sources”选项卡中显示为filename.js In my experience you can use \\'s in the name but I get odd behaviour if using /'s. 根据我的经验,你可以在名字中使用\\,但如果使用/的话,我会得到奇怪的行为。

For more information see: Breakpoints in Dynamic JavaScript deprecation of //@sourceurl 有关更多信息,请参阅: // @ sourceurl的 动态JavaScript 弃用中的 断点

You can use //# sourceURL= and //# sourceMappingURL= at the end of your script file or script tag. 您可以在脚本文件或脚本标记的末尾使用//# sourceURL=//# sourceMappingURL=

NOTE: //@ sourceURL and //@ sourceMappingURL are deprecated. 注意: //@ sourceURL//@ sourceMappingURL已弃用。

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. 我尝试使用OP建议作为解决方法的“//#sourceURL = filename.js”,但它仍然没有在Sources面板中显示,除非它在我之前的选项卡中已经存在它产生了一个例外。

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;" 然后,一旦它出现在Sources面板中的标签中,我可以设置正常的断点并删除“调试器”; line. 线。

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; 请注意,以这种方式出现在sources选项卡中的源文件将出现在(no domain)组中,如果要调试它,则需要添加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; 如果您正在调试生产阶段,那么您可能没有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". 代码中的行,您可以通过使用CharlesProxy将本地映射添加到“插入了debbuger行的源文件的新副本”来实现。

When trying to track this sort of thing down in IE, I open the dev tools (F12) and then find where to place the breakpoint by using the following line in the console: 当尝试在IE中跟踪此类事物时,我打开开发工具(F12),然后通过在控制台中使用以下行找到放置断点的位置:

debugger;myFunction();

That switches to the debugger tab where you can step into myFunction() and set the breakpoint. 切换到调试器选项卡,您可以在其中进入myFunction()并设置断点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何找出/调试将JavaScript加载到浏览器的内容 - How to figure out/debug what loaded a javascript into browser 如何使用JavaScript / jQuery根据浏览器的窗口大小在CSS类中动态设置高度? - How to dynamically set the height in a CSS-class depending on the browser's window size with Javascript / jQuery? 如何获取动态加载的HTML以使用Javascript Jquery进行解析? - How to get dynamically loaded HTML to be parsed using Javascript Jquery? 如何知道是否所有图片都是动态加载添加的? javascript,jquery - How to know if all images are added dynamically loaded? javascript, jquery 如何在jquery或javascript中访问动态加载的元素? - How do I access a dynamically loaded element in jquery or javascript? 如何知道浏览器选项卡是否在加载Javascript时关注? - How to know if a browser tab focused when it's loaded with Javascript? 如何使用V8调试器调试MongoDB JavaScript - How to debug MongoDB JavaScript with V8 debugger 如何在Safari调试器中调试延迟加载的JavaScript - How to debug lazily-loaded javascripts in Safari debugger 如何在客户端的浏览器/操作系统/系统上远程调试jQuery应用程序? - How to debug a jQuery application remotely on client's browser/OS/system? 如何调试动态加载页面的 JS? - How to debug JS of a dynamically loaded page?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM