4
votes

I know this seems like a simple question, but I have discovered there is no simple way of getting comment count for a given disqus identifier in an ajax page.

I have looked at their API, and this is an option, but we are building an ajax cms-based website for end-users, and it would seem a bit tedious to force each user to have to create their own disqus application API and fill in public and secret keys just to get comments count. Besides, it seems overkill to load a separate remote JS, returning an entire JSON object, just to get the current page comments count.

There is a count.js script here, but there is no information about how to update count dynamically for ajax pages. Well almost ... after a lot of searching, I found some undocumented method DISQUSWIDGETS.getCount(). However, this stops working after one call for each identifier. Besides, this method also requires loading an external JS just to get comments count ...

Seems strange #comments amount cannot be extracted easier. The amount of comments is after all available after comments are displaying on the page, but we can't access that iframe with JS of course. Any enlightenments are appreciated ...

1
Disqus is one of the worst services I've ever used. Their APIs are either non-existent, don't work, or only work once (!?!?!). Apparently DISQUS.getCommentCounts() was just too obvious for them to implement.AJB

1 Answers

0
votes
 This is the html file which will help you to count the number of comments in a particular node in for any site which has disqus comment.

  <!DOCTYPE html>
   <html>
    <head>
      <title>Disqus Comment Counts Example</title>
    <script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
   <script type="text/javascript">
   $(document).ready(function () {
   var disqusPublicKey = "?";

   var disqusShortname = "?"; // Replace with your own shortname

  var urlArray = [];

    $('.count-comments').each(function () {
      var url = $(this).attr('data-disqus-url');
      urlArray.push('link:' + url);
       });



    $('#get-counts-button').click(function () {
          $.ajax({
           type: 'GET',
               url: "https://disqus.com/api/3.0/threads/set.jsonp",
                   data: { api_key: disqusPublicKey, forum : disqusShortname, thread : urlArray },
cache: false,
dataType: 'jsonp',
success: function (result) {

  for (var i in result.response) {

    var countText = " comments";
    var count = result.response[i].posts;

    if (count == 1)
      countText = " comment";

    $('div[data-disqus-url="' + result.response[i].link + '"]').html('<h4>' + count +      countText + '</h4>');

       }
    }
    });
 });


  });
  </script>
    </head>
       <body>
    <h1>Comment Counts Example</h1>
    <div>
        <a href="#">
            <h2>Fullscreen BEAM: The first YouTube app for Google Glass comes with    public or private sharing</h2>
              <div class="count-comments" data-disqus-     url="http://www.dev.indiawaterportal.org/questions/can-using-ro-water-result-stomach-problems"></div>
        </a>
    </div>

       <button type="button" id="get-counts-button">Get Comment Counts</button>
   </body>