Skip to content

Instantly share code, notes, and snippets.

@CrocoDillon
Created June 7, 2013 08:49
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save CrocoDillon/5727950 to your computer and use it in GitHub Desktop.
Save CrocoDillon/5727950 to your computer and use it in GitHub Desktop.
/*
* http://christian-fei.com/tutorials/how-to-lazy-load-disqus-comments/
*
* <div class="comments"></div>
*/
var comments = document.getElementsByClassName('comments')[0],
disqusLoaded = false;
function loadDisqus() {
var disqus_shortname = 'your_disqus_shortname';
var dsq = document.createElement('script');
dsq.type = 'text/javascript';
dsq.async = true;
dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
disqusLoaded = true;
}
//Get the offset of an object
function findTop(obj) {
var curtop = 0;
if (obj.offsetParent) {
do {
curtop += obj.offsetTop;
} while (obj = obj.offsetParent);
return curtop;
}
}
if(window.location.hash.indexOf('#comments') > 0)
loadDisqus();
if(comments) {
var commentsOffset = findTop(comments);
window.onscroll = function() {
if(!disqusLoaded && window.pageYOffset > commentsOffset - 1000)
loadDisqus();
}
}
@samdoidge
Copy link

Thanks bro, still works.

@alvarotrigo
Copy link

The only problem with that approach is that comments won't get indexed by google. Which otherwise I believe they are, as google also recognise stuff after page load (but it seems not after a scroll event?)
Correct me if I'm wrong, but I'm using a similar technique in my blog and none of the comments are indexed by google (related with my blog, some are indexed in the diqus page)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment