Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Cannot read property 'appendChild' of null" with Disqus on Backbone website

I have a website on Backbone. When I try to execute Disqus code i get

Uncaught TypeError: Cannot read property 'appendChild' of null

How can I fix it? Why is this happening?

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);
})();

console:

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
like image 664
derzunov Avatar asked Jun 01 '15 13:06

derzunov


1 Answers

For those just finding this in 2015, in addition to occurring when "head" or "body" is missing, this error will occur when you do not have the following div somewhere in your page:

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

Put that div where you want the disqus thread to actually appear.

like image 168
Tom Boutell Avatar answered Nov 04 '22 21:11

Tom Boutell