How to Load Disqus Comments on Click

The comments on my website are powered by Disqus, the most popular commenting platform that offers a lot more features than what the native commenting engines of Blogger or WordPress have to offer. For instance, Disqus lets me moderate discussions or reply to comments via email itself and commenters can use their existing Facebook or Twitter accounts to sign-in for commenting on web pages.

The Disqus widget is loaded asynchronously meaning it downloads the JavaScript in parallel and would not therefore impact the load time of your web pages. That said, the widget still adds lot of weight to your pages as the Disqus files will download on the user’s computer even if they aren’t interested in participating in the discussion. The other issue with auto-loading Disqus is that it makes your pages lengthy especially when viewed on mobile devices.

disqus comments

Load Disqus on Demand with JavaScript

As an alternative, you can configure Disqus on your website to load on-demand and not automatically. When someone clicks a button - like the example here - the widget will be dynamically added to your web page and not otherwise. This lazy-loading technique can be implemented in pure JavaScript without jQuery.

Step 1: Go to your web page template that has Disqus and replace the #disqus_thread <div> with the following snippet:

<div id="disqus_thread">
  <a href="#" onclick="disqus();return false;">Show Comments</a>
</div>

Step 2: Next place the Disqus code before the close <head> tag of your web page. You’ll have to replace the disqus variables - like disqus_shortname, disqus_url, etc. - with your own parameters.

<script type="text/javascript">

// Replace labnol with your disqus shortname
var disqus_shortname = "labnol";

// Put the permalink of your web page / blog post
var disqus_url = "http://example.com/blog-post";

// Put the permalink of your web page / blog post
var disqus_identifier = "http://example.com/blog-post";

var disqus_loaded = false;

// This is the function that will load Disqus comments on demand
function disqus() {

  if (!disqus_loaded)  {

    // This is to ensure that Disqus widget is loaded only once
    disqus_loaded = true;

    var e = document.createElement("script");
    e.type = "text/javascript";
    e.async = true;
    e.src = "//" + disqus_shortname + ".disqus.com/embed.js";
    (document.getElementsByTagName("head")[0] ||
     document.getElementsByTagName("body")[0])
    .appendChild(e);
  }
}

</script>

The page will have a “Show Comments” button and the comments are only loaded when the button is clicked.

Some websites have auto-loading enabled for Disqus but the widget is loaded when the reader has scrolled to the bottom of the  article. This can again be done in JavaScript. We can use the onscroll method to check whenever the page is scrolled and if the user is near the bottom, the script will load the Disqus widget.

Place this snippet near the closing </body> tag of your page.

<script type="text/javascript">
  window.onscroll = function (e) {
    if (window.innerHeight + window.scrollY >= document.body.offsetHeight) {
      if (!disqus_loaded) disqus();
    }
  };
</script>
Amit Agarwal

Amit Agarwal

Google Developer Expert, Google Cloud Champion

Amit Agarwal is a Google Developer Expert in Google Workspace and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India.

Amit has developed several popular Google add-ons including Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

0

Awards & Titles

Digital Inspiration has won several awards since it's launch in 2004.

Google Developer Expert

Google Developer Expert

Google awarded us the Google Developer Expert award recogizing our work in Google Workspace.

ProductHunt Golden Kitty

ProductHunt Golden Kitty

Our Gmail tool won the Lifehack of the Year award at ProductHunt Golden Kitty Awards in 2017.

Microsoft MVP Alumni

Microsoft MVP Alumni

Microsoft awarded us the Most Valuable Professional (MVP) title for 5 years in a row.

Google Cloud Champion

Google Cloud Champion

Google awarded us the Champion Innovator title recognizing our technical skill and expertise.

Email Newsletter

Sign up for our email newsletter to stay up to date.

We will never send any spam emails. Promise.