How to insert more Disqus comments box in a single page

Hello everyone!

For those not familiar Disqus is a platform that allows you to easily insert a box for comments on any website and allows users to use the their Facebook or Twitter account (but not only) to authenticate.

The limit of Disqus is that it associates any thread of comments to a unique url.
Consequently, it will expect that in every page there is only a comment box.

In most cases this limit is acceptable, but for some applications it is useful to put more comments boxes on a single page. As an example think about a blog where you want to give the opportunity to comment on each post directly from the main page which lists all the posts one under each other.

What I want to show you today is a method to overcome the limit of Disqus and easily implement a page with more comments boxes, as in the blog example.
Using an iframe and jQuery we will make the boxes also dynamically loaded.

Suppose that we have our page index.html.haml as follows and to already have a Disqus account:

1
2
3
4
- @posts.each do |post|
  = show_for post do |s|
    = s.attribute :title
    = s.attribute :body

The idea is to create an iframe dynamically below each post and have it displayed / hidden by jQuery when you click on a link “comments”.

We start creating the page comments.html.haml that we want to load inside the iframe.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#disqus_thread

%noscript
  Please enable JavaScript to view the
  %a{:href => "http://disqus.com/?ref_noscript"} comments powered by Disqus.

%a.dsq-brlink{:href => "http://disqus.com"}
  blog comments powered by
  %span.logo-disqus Disqus


:javascript
  var disqus_shortname = 'devinterface-example';

  (function() {
      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);
  })();

  function disqus_config() {
    this.callbacks.onReady = [function() {
      var frameHeight = $(document).height();
      $('iframe.comment_frame', top.document).attr("height", frameHeight)
    }];
    this.callbacks.onNewComment = [function() {
      var frameHeight = $(document).height();
      $('iframe.comment_frame', top.document).attr("height", frameHeight)
    }];
  }

On this page, we insert the html and javascript provided by Disqus and we set the variable disqus_shortname.
Then, using the disqus_config function we set two callbacks for Disqus so that after the complete loading or aftere a new comment is inserted, the iframe that contains this page be appropriately resized
NOTE: resizing iframe “container” is only possible because the two page “index” and “comments” are on the same domain. If not, in fact, access to the parent of an iframe would be impossible for the security policy of browsers.

Now that we have created our comments page, we have only to add the iframe dynamically to the index page.
To make things more “cool” we use the count of comments for each post as the link that will open/close the comments box.

Now we return to the index page and modify it as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
- @posts.each do |post|
  = show_for post do |s|
    = s.attribute :title
    = s.attribute :body
    / disqus comments
    = link_to "", "/#{post.slug}/comments#disqus_thread", :class=> "dq_comment", :id => post.id
    .comment_box{:id => "comment_#{post.id}"}

:javascript
  var disqus_shortname = 'devinterface-example';

  (function () {
      var s = document.createElement('script'); s.async = true;
      s.type = 'text/javascript';
      s.src = 'http://' + disqus_shortname + '.disqus.com/count.js';
      (document.getElementsByTagName('HEAD')[0] || document.getElementsByTagName('BODY')[0]).appendChild(s);
  }());

  $(document).ready(function(){
     $(".dq_comment").click(function(e){
        e.preventDefault();

        var id = $(this).attr("id");
        var href = $(this).attr("href");
        var src = href.substring(0,href.length-14); // Remove #disqus_thread

        if ($("#comment_"+id).is(':visible'))
          $("#comment_"+id).html('');
        else
          $("#comment_"+id).append('<iframe scrolling="no" frameborder="0" src="'+src+'" class="comment_frame"></iframe>');

        $("#comment_"+id).toggle('slow');
     })
  });

As you can see we have added the link with the unique url of the post and the tag # disqus_thread to activate the counter of the comments, as explained in the documentation.
Finally, using the jQuery function we intercepted the click on the link in such a way to dynamically create an iframe below the post with the url of our comments.html.haml page.
Obviously the comments page’s url have to be appropriately defined in our routes file.

Now you just need to open your browser and see your posts, each with its comments box dynamically loaded by clicking on the counter link.

NOTE: If you try these scripts on localhost, remember to add to the two Disqus javascript:

1
 var disqus_developer = 1;

just below disqus_shortname.

Tags: , , , ,


About Claudio

Claudio Marai is a co-founder of DevInterface.

After graduating in Computer Science has contributed to develop complex web applications based on Java/J2EE and desktop applications with the. NET framework for the Ministry of Justice and ultimately for the banking ambit.

The passion for web in recent years has led him to be interested in more modern frameworks such as Ruby on Rails and Django, and to a development approach based on agile methodologies such as eXtreme Programming and SCRUM.

About DevInterface

We are an information and communication technology agency. Our mission is to provide web application development, design services and communication strategies. We specialize in building web applications with modern and efficient frameworks.

Related Post

3 Responses to “How to insert more Disqus comments box in a single page”

  1. Tecnología says:

    Interesante articulo ,todo muy bien explicado y detallado..

  2. fantastic quality blog post, i really appreciate it, maintain up this excellent work

  3. google says:

    Thank-you for your kindness in sharing this knowledge. The marketing strategies has always changed year by year. These was real thought-provoking post!

Leave a Reply

Insert code beetween <code lang="ruby"> and </code>

Copyright 2012 DevInterface s.n.c.

DevInterface Blog is proudly powered by WordPress