【问题标题】:Identify which Javascript executed a call using Firebug or Google Chome识别使用 Firebug 或 Google Chrome 执行调用的 Javascript
【发布时间】:2012-03-29 17:47:39
【问题描述】:

如何知道哪个 Javascript (.js) 使用 Firebug 或 Google Chrome 插件执行了 GET 或 Ajax 调用?

例如,一个 Javascript 执行的对 imagehtml 文件的请求,如何知道页面上的哪个 Javascript 执行了该文件

【问题讨论】:

    标签: javascript firefox google-chrome firebug


    【解决方案1】:

    更新:

    我不得不(可耻地)承认下面的原文是错误的。由于js执行流程的性质this works at the first execution time

    console.log($("script").last().attr("class")); //or whatever
    

    然而,这还不够好,如超时调用所示。我们需要保留对脚本元素的引用,this can be achieved by wrapping script contents into a closure and creating a variable to store the jQuery reference:

    (function(){
        var $scriptElement = $("script").last();
        console.log($scriptElement.attr("class")); //or whatever
    })();
    

    现在,我不得不声明,使用上面的标记,除非你想出一个更好的方法来存储脚本元素引用,否则它不太可能是实用的......这几乎成了一个相当糟糕的例子 可以做,但真的不应该:(

    除非您必须在代码中获取参考,否则您最好查看控制台输出,它实际上会告诉您输出的来源,以及行号和所有内容:

    原文:

    不确定这对外部 js(带有 src 的脚本标签)有多适用,但对于内联脚本,您可以使用 jQuery 执行类似的操作:

    $(this).closest("script");//gets you reference to the script element
    

    我假设这只是获取其 src 属性的问题!让我们知道它是否有效。

    console.log($(this).closest("script").attr("src"));
    

    【讨论】:

    • @xybrek:我应该知道最好不要在没有验证的情况下发布答案。已编辑,可能以令人失望的方式进行。
    【解决方案2】:

    在 chrome 中,您可以中断任何 xhr 请求。不过,这只会为 AJAX 调用设置断点。

    【讨论】:

      猜你喜欢
      • 2013-01-10
      • 2012-05-25
      • 2010-09-23
      • 2014-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多