admin管理员组文章数量:1193750
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/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
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
Share
Improve this question
edited Jun 1, 2015 at 14:41
derzunov
asked Jun 1, 2015 at 13:46
derzunovderzunov
4524 silver badges13 bronze badges
1
- seems you have not define either head or body tag in your HTML. – FatalError Commented Jun 1, 2015 at 13:48
3 Answers
Reset to default 21For 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.
It appears your document is missing both a head
and a body
for some reason.
Try this:
(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 in the console.
I've solved 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);
}
Thx to @boutell and @June for the clue.
本文标签:
版权声明:本文标题:javascript - "Cannot read property 'appendChild' of null" with Disqus on Backbone website - St 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738490270a2089663.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论