Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/431.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 &引用;无法读取属性';追加儿童';“无效”的定义;在主干网站上发布论文_Javascript_Backbone.js_Disqus - Fatal编程技术网

Javascript &引用;无法读取属性';追加儿童';“无效”的定义;在主干网站上发布论文

Javascript &引用;无法读取属性';追加儿童';“无效”的定义;在主干网站上发布论文,javascript,backbone.js,disqus,Javascript,Backbone.js,Disqus,我在主干网上有一个网站。 当我尝试执行DISKS代码时,我得到 未捕获的TypeError:无法读取null的属性“appendChild” 我怎样才能修好它?为什么会这样 var disqus_shortname = 'mysite'; /* * * DON'T EDIT BELOW THIS LINE * * */ (function() { var dsq = document.createElement('script'); dsq.type = 'text/javascript'

我在主干网上有一个网站。 当我尝试执行DISKS代码时,我得到

未捕获的TypeError:无法读取null的属性“appendChild”

我怎样才能修好它?为什么会这样

var disqus_shortname = 'mysite';

/* * * DON'T EDIT BELOW THIS LINE * * */
(function() {
  var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
  dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
控制台:

undefined
embed.js:1 Unsafe attempt to redefine existing module: BASE
embed.js:1 Unsafe attempt to redefine existing module: apps
embed.js:1 Unsafe attempt to redefine existing module: get
...

embed.js:1 Unsafe attempt to redefine existing module: configAdapter
embed.js:1 Unsafe attempt to redefine existing module: removeDisqusLink
embed.js:1 Unsafe attempt to redefine existing module: loadEmbed
embed.js:1 Unsafe attempt to redefine existing module: reset
embed.js:1 Uncaught TypeError: Cannot read property 'appendChild' of null

由于某种原因,您的文档似乎同时缺少
标题
正文

试试这个:

(function() {
    var dsq = document.createElement('script');
    var head = document.getElementsByTagName('head')[0];
    var body = document.getElementsByTagName('body')[0];

    dsq.type = 'text/javascript';
    dsq.async = true;
    dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';

    console.log('head', head);
    console.log('body', body);

    (head || body).appendChild(dsq);
}());

然后查看控制台

对于那些在2015年才发现这一点的人来说,除了在缺少“head”或“body”时发生外,当您的页面中没有以下div时,还会发生此错误:

<div id="disqus_thread"></div>

将该div放在您希望discus线程实际出现的位置。

我已经这样解决了:

// Only if disqus_thread id is defined load the embed script
if (document.getElementById('disqus_thread')) {
    var your_sub_domain = ''; // Here goes your subdomain
    var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
    dsq.src = '//' + your_sub_domain + '.disqus.com/embed.js';
    (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
}

Thx至@boutell和@June以获取线索。

似乎您尚未在HTML中定义head或body标记。但仍然记录“Uncaught TypeError:无法读取属性'appendChild'为null”此错误(来自embed.js屏幕截图)-是的,我们需要更多信息来解决您的问题,此答案是为了帮助您诊断错误。在错误上方,您是否看到“头部”或“身体”旁边的内容?如果您直接在控制台中编写
document.getElementsByTagName('head')[0]
,您会看到什么?head和body都存在。好的,但是从该函数中登录时
head
body
是否存在?这为我解决了一个类似的问题。我有一个“appendChild of null”错误,我刚刚意识到我在所有地方都包含了disqs脚本,但没有在每个页面上都有注释。现在我只是将代码包装在
if($(“#discus_thread”).length){…}
中,我很好。