How to Load Disqus onClick Event

Disqus commenting system is a great service. It allows you to seamlessly and very easily integrate a full-featured comments section in your blog or website. It has eliminated the need of maintaining user comments on your own server. Disqus takes care of all the comments made on your website and keep them associated with appropriate pages. It loads comments from Disqus server in asynchronous fashion using AJAX.

But there is a problem! When a page on your website loads and demands associated comments from Disqus server, it has to make a lot of GET queries. All the comments, gravatars of commentators, support files etc, all this has to come all the way from Disqus server on to your page. As a result, the page load speed of your website decreases.

Wouldn’t it be great if you could load Disqus comments on demand? For example, it would be excellent to load comments when a user clicks on a button. If user does not want to read or post comments, then there is no need to unnecessarily fetch comments from Disqus. Such on demand load of Disqus comments can be achieved by exploiting onClick event, asynchronous loading of Disqus JavaScript and/or AJAX. Let’s see how to do this.

Er… wait a second… do you first want to see the demo? Well, then just scroll down this page and see for yourself. At the time of writing this article, I am also using this method of loading Disqus comments on click event.

OK, now let’s see how to do this.

Remove comments Section of your website

First of all, you should do basic integration of Disqus Commenting System in your website or blog. If you’re using WordPress platform, you can easily do this integration using a plugin provided by Disqus. Otherwise also it is just a matter of including a JavaScript code provided by Disqus.

Needless to say, if you’re integrating Disqus, you should remove comments section, in case it is provided by your CMS or blog platform.

Load Disqus on click without using jQuery

This is the plain method that relies on basic JavaScript. If your website is not using jQuery (I wonder why!), you can use this simple method. Here is the code:

HTML

<div id="disqus_thread"></div>
<div id="disqus-comments">
<button onclick="load-disqus()">Read Comments</button>
</div>

JavaScript

function load-disqus() {
var dsq = document.createElement('script');
dsq.type = 'text/javascript';
dsq.async = true;
dsq.src = "http://[YOUR-DISQUS-SHORTNAME].disqus.com/embed.js";
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
var load-button = document.getElementById('disqus-comments');
load-button.parentNode.removeChild(load-button);
}

Please note the following:

  1. You must not change the id=”disqus_thread” as it is used by the commenting system for certain operations.
  2. Replace [YOUR-DISQUS-SHORTNAME] with you own Disqus shortcode (you get it when you add a website into your Disqus account).
Load Disqus on click using jQuery

As most of the blogging platforms and other websites do load jQuery, you may want to take advantage of this excellent JavaScript package. Here is the code for loading Disqus on demand using jQuery and AJAX.

HTML

<div id="disqus_thread"></div>
<div id="disqus-comments">
<button onclick="load-disqus()">Read Comments</button>
</div>

JavaScript

$(document).ready(function() {
$('#loadDisqusBtn').on('click', function(){
var disqus_shortname = 'YOUR-DISQUS-SHORTNAME';
		$.ajax({
	         type: "GET",
	         url: "http://" + disqus_shortname + ".disqus.com/embed.js",
	         dataType: "script",
	         cache: true
	     });
	        $(this).fadeOut();
	});
});

In this code also, please don’t forget to replace [YOUR-DISQUS-SHORTNAME] with your own shortcode. This script uses AJAX to get Disqus JavaScript from server. Once the click event is triggered, the event causing button disappears. I am using this method to load comments on demand.

Using a Plugin

Those who are not code-savvy people, they can make use of a WordPress plugin to load comments on click of a button. This method is not recommended because installing too many plugins also slow down your website. So, you try to avoid installing plugins whose work you can easily do on your own by tweaking code a bit.

Advantages of loading Disqus on click

There are several advantages of using either of the given method. Let me explain:

  • Page load speed of your webpage increases. It takes lesser time to load and therefore, your website gains brownie points both from visitors and search engines.
  • Your page looks much cleaner without the clutter of comments section.
  • Total length of the page also decreases, which makes it easier for the visitor to scroll up and down the content.
  • It will reduce comment spam. You’ll get no spam at all. If you use WordPress default comment section without captcha and/or Akismet plugin —you’re likely to get loads of spam comments. Using on demand Disqus, will leave no scope for spammer as there will not be a form available for them to fill. Comment form appears only when person clicks on the button.
Disadvantages of loading Disqus on click

There is only one disadvantage that I can think of. It’s a matter visitor psychology “out of sight, out of mind”. If a visitors sees comments right served up to him, he may be more likely to read them. Contribution in comments section is also more likely if comments section is seen by default.

But if your content is so good that visitors will not bother to make an extra click for commenting, you should use this method.

2 thoughts on “How to Load Disqus onClick Event”

    1. Hi Clara, for an experiment I have disabled the selection of text on TechWelkin. You’ll have to type the code manually. I apologize for the inconvenience.

Leave a Comment

Your email address will not be published. Required fields are marked *