首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >骨干网站上的Disqus“无法读取属性'appendChild‘of null”

骨干网站上的Disqus“无法读取属性'appendChild‘of null”
EN

Stack Overflow用户
提问于 2015-06-01 13:46:03
回答 3查看 9.8K关注 0票数 8

我有一个骨干网站。当我尝试执行Disqus代码时,我得到

Uncaught :无法读取属性'appendChild‘的null

我怎么才能修好它?为什么会发生这种情况?

代码语言:javascript
复制
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);
})();

控制台:

代码语言:javascript
复制
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
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-06-01 13:54:04

由于某种原因,您的文档似乎同时缺少了headbody

试试这个:

代码语言:javascript
复制
(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);
}());

那就看看控制台。

票数 3
EN

Stack Overflow用户

发布于 2015-08-04 19:30:22

对于那些在2015年才发现这一点的人,除了在"head“或"body”丢失时发生外,如果页面中没有以下div,则会发生此错误:

代码语言:javascript
复制
<div id="disqus_thread"></div>

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

票数 21
EN

Stack Overflow用户

发布于 2017-04-06 19:23:47

我就这样解决了:

代码语言:javascript
复制
// 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);
}

以“boutell”和“June”为线索。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30575237

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档