如何在浏览器的调试器本身中调试动态加载的 JavaScript(使用 jQuery)? [英] How to debug dynamically loaded JavaScript (with jQuery) in the browser's debugger itself?

查看:27
本文介绍了如何在浏览器的调试器本身中调试动态加载的 JavaScript(使用 jQuery)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

动态添加的脚本未显示在浏览器的调试器脚本部分.

A dynamically-added script is not showing up in the browser's debugger's scripts section.

说明:

我需要使用并且已经使用过

I need to use and have used

if( someCondition == true ){
   $.getScript("myScirpt.js", function() {
       alert('Load Complete');
       myFunction();
   });
}

这样可以在满足某些条件时动态加载 myScript.js...并且 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.

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?

推荐答案

您可以为动态加载的脚本命名,以便它在 Chrome/Firefox JavaScript 调试器中显示.为此,请在脚本末尾添加注释:

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

此文件将显示在来源"中.标签为 filename.js.根据我的经验,您可以在名称中使用 's,但如果使用/'s,我会出现奇怪的行为.

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.

如果没有指定域,filename.js 将出现在名为(无域)"的文件夹中;指定一个域来提高调试体验是很方便的,例如看到一个自定义"域.可以使用的文件夹:

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

一个完整的例子如下:

window.helloWorld = function helloWorld()
{
  console.log('Hello World!');
}
//# sourceURL=browsertools://custom/helloWorld.js

有关更多信息,请参阅:动态 JavaScript 中的断点弃用//@sourceurl

For more information see: Breakpoints in Dynamic JavaScript deprecation of //@sourceurl

这篇关于如何在浏览器的调试器本身中调试动态加载的 JavaScript(使用 jQuery)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆