我有一个骨干网站。当我尝试执行Disqus代码时,我得到
Uncaught :无法读取属性'appendChild‘的null
我怎么才能修好它?为什么会发生这种情况?
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发布于 2015-06-01 13:54:04
由于某种原因,您的文档似乎同时缺少了head和body。
试试这个:
(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-08-04 19:30:22
对于那些在2015年才发现这一点的人,除了在"head“或"body”丢失时发生外,如果页面中没有以下div,则会发生此错误:
<div id="disqus_thread"></div>将div放在您希望disqus线程实际出现的位置。
发布于 2017-04-06 19:23:47
我就这样解决了:
// 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”为线索。
https://stackoverflow.com/questions/30575237
复制相似问题