2015-06-01 18 views
5

Ho un sito Web su Backbone. Quando provo ad eseguire codice Disqus ottengo"Impossibile leggere la proprietà 'appendChild' di null" con Disqus sul sito Web di Backbone

Uncaught TypeError: Cannot read property 'appendChild' of null

Come posso risolvere il problema? Perché sta succedendo?

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 
+0

sembra che tu non abbia definito tag ** head ** o ** body ** nel tuo codice HTML. – FatalError

risposta

1

Sembra il documento non è presente sia un head e body per qualche ragione.

Prova questo:

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

quindi cercare nella console.

+0

Registra la testa e il corpo – derzunov

+0

ma registra ancora "Uncaught TypeError: Impossibile leggere la proprietà 'appendChild' di null" – derzunov

+0

questo errore dallo screenshot di embed.js - https://cloud.mail.ru/public/9iFg/8t8pomZXd – derzunov

15

Per coloro che solo trovare questo nel 2015, oltre a che si verifica quando "testa" o "corpo" manca, questo errore si verifica quando non si ha il seguente div da qualche parte nella tua pagina:

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

Metti quel div dove vuoi che appaia effettivamente il thread di disqus.

+5

Questo ha risolto un problema simile per me. Ho avuto l'errore "appendChild of null" e ho appena realizzato di aver incluso lo script disqus ovunque, ma non avevo i commenti su ogni pagina. Ora ho appena avvolto il codice in 'if ($ (" # disqus_thread "). Length) {...}' e sto bene. – June

1

ho risolto in questo modo:

// 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 a @boutell e @June per l'indizio.

Problemi correlati