Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 通过jQuery添加的脚本标记在FireBug中不可见_Javascript_Jquery_Debugging_Firebug - Fatal编程技术网

Javascript 通过jQuery添加的脚本标记在FireBug中不可见

Javascript 通过jQuery添加的脚本标记在FireBug中不可见,javascript,jquery,debugging,firebug,Javascript,Jquery,Debugging,Firebug,我正在通过jQuery向文档头添加。这是我使用的代码: $(document).ready(function () { var s = document.createElement("script"); s.type = "text/javascript"; s.src = (document.location.protocol == "https:" ? "https://ssl" : "http://www") + ".google-analytics.com/ga.

我正在通过jQuery向文档头添加
。这是我使用的代码:

$(document).ready(function () {
    var s = document.createElement("script");
    s.type = "text/javascript";
    s.src = (document.location.protocol == "https:" ? "https://ssl" : "http://www") + ".google-analytics.com/ga.js";
    $("head").append(s);
});
虽然脚本似乎工作得很好,但当我使用FireBug检查文档头时,我并没有在头部看到脚本。此代码段也不显示添加的脚本:

这是正常的还是我做错了什么?让我烦恼的是,我在head部分看到了其他延迟/动态加载的脚本,但没有看到我添加的脚本。还想知道在DocumentReady函数中加载操纵DOM的脚本是否可以

更新 从以下位置替换代码:

$("head").append(s);


解决了这个问题。生成的DOM在FireBug中正确显示,jQuery正确返回静态/动态添加的脚本标记。

您将在
NET
选项卡中看到对脚本的请求,但在检查DOM时,
脚本
标记将不可见。这看起来像是FireBug中的一个错误。

在Chrome中测试它,并使用右键单击“检查元素”选项使用完整的调试器(查看源代码不会显示脚本的修改)。elements HTML选项卡应该显示对DOM的实时更改

好的,我在jQuery.com上找到:

>应该注意的是,任何使用此
>方法将以静默方式失败:
>$('#element')。追加(“”);
>>不完全是。脚本将首先被评估,然后被丢弃。
>>因此,如果您这样做:
>>$('#element')。追加(“警报('hello');”;
>>你会看到警报。

这可能意味着脚本已计算,但未插入DOM。

这是Mozilla“jsd”调试器支持中的一个错误。一种解决方法是在上面提到的bug上发布:


如果jquery使用eval()而不是脚本标记注入,那么您可以在Firebug中对此进行调试。

我相信这是正常的。Firebug说“警告。页面加载期间脚本面板处于非活动状态-重新加载以查看所有源”。这表明只能调试页面加载时可用的脚本。jQuery专家是否可以验证通过
$添加的
。append
方法不会实际添加到DOM中,而只是对它们进行
eval
评估?为什么不使用$.getScript()?我认为默认情况下,
$。getScript
试图避免浏览器缓存,我不希望这种情况发生。请尝试
$(document.head).append(s)。这是否意味着jQuery也无法选择脚本标记?我的错误是,我在考虑Firefox w/Firebug中的“Inspect元素”。我现在正在一个带有幻灯片的页面上测试它,每次幻灯片切换firebug都会更新HTML选项卡以显示当前DOM,并突出显示修改后的元素约1s
$("head").append(s);
document.getElementsByTagName("head")[0].appendChild(s);