Skip to content

Instantly share code, notes, and snippets.

@anandkumar
Last active December 23, 2015 10:19
Show Gist options
  • Save anandkumar/6620947 to your computer and use it in GitHub Desktop.
Save anandkumar/6620947 to your computer and use it in GitHub Desktop.
disqus lazy load
<script type="text/javascript">
var comments = document.getElementsByClassName('comments')[0],
disqusLoaded=false;
function loadDisqus() {
var disqus_shortname = 'netrival';
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 - 1500) {
console.log('load comments, NOW!!');
loadDisqus();
}
}
}
</script>
/*** Disqus Comment for Genesis Framework ***/
remove_action( 'genesis_comments', 'genesis_do_comments' );
add_action('genesis_comments', 'wpa_disqus_comments' );
function wpa_disqus_comments() {
if ( is_single() ) {
?>
<aside id="comments" class="entry-comments comments">
<h3>Comments</h3>
<div id="disqus_thread"></div>
<script type="text/javascript">
var disqus_url = "<?php the_permalink(); ?>";
var disqus_identifier = "<?php the_ID(); ?> <?php echo home_url('/'); ?>?p=<?php the_ID(); ?>";
</script>
<noscript><a href="http://disqus.com/?ref_noscript">Please wait, Comments are being loaded</a></noscript>
<a href="http://disqus.com" class="dsq-brlink">Please wait, Comments is being loaded</a>
</aside>
<?php }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment