esedic
11/15/2013 - 10:43 AM

Load Disqus comments only on demand if you give a shit about page weight and your visitors. Even with no comments, i.e. an empty comment

Load Disqus comments only on demand if you give a shit about page weight and your visitors.

Even with no comments, i.e. an empty comment form, calling Disqus will load an extra 226Kb. If your page has comments this can be far higher.

This Gist accompanies my blog post: http://internet-inspired.com/wrote/load-disqus-on-demand/

<!-- An element a visitor can click if they <3 comments! -->
<button class="show-comments">Load Disqus comments</button>

<!-- The empty element required for Disqus to loads comments into -->  
<div id="disqus_thread"></div>
// Requires jQuery of course.
$(document).ready(function() {
    $('.show-comments').on('click', function(){
          var disqus_shortname = 'YOUR-DISQUS-USERNAME'; // Replace this value with *your* username.

          // ajax request to load the disqus javascript
          $.ajax({
                  type: "GET",
                  url: "http://" + disqus_shortname + ".disqus.com/embed.js",
                  dataType: "script",
                  cache: true
          });
          // hide the button once comments load
          $(this).fadeOut();
    });
});