Msgstr "Cannot read the appendChild" null "property with the Disqus on Backbone site.

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

Uncaught TypeError: Cannot read the appendChild property from null

How can i fix this? 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); })(); 

Prefixes:

 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 
+5
source share
3 answers

For some reason, your document lacks both head and body .

Try the following:

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

Then look into the console.

+1
source

For those who just find this in 2015, besides the fact that the β€œhead” or β€œbody” occurs, this error occurs if your page does not have the following div:

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

Place this div where you want the disqus stream to actually display.

+15
source

I decided like this:

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

thanks to @boutell and @June for the tip.

+1
source

All Articles