Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Is it possible to reverse the order in which comments appear?

Bizarre request I know, but, I've been asked if it is possible to reverse the order of comments, ie.e. have the most recent at the top?

12 answers

1 accepted

You can do that client-side with a quick JavaScript hack...

Browse to Confluence Admin | Look & Feel | Custom HTML

Add this to At the end of the HEAD:

<script>
  AJS.toInit(function () {
    $comments = AJS.$('#page-comments'); 
    $comments.children().each(function (i,li) {
$comments.prepend(li);
}); }); </script>

Check the related blogpost for more tips :)

 

 

Thanks Dave, that did it.

Cheers

Norman

No problem. If it worked, please mark it as correct :-)

Also, if you want to allow the sort order to be changed, update the JavaScript to something like...

function reverseCommentOrder () {
  var $comments = AJS.$('#page-comments'); 
  $comments.children().each(function(i,li){$comments.prepend(li)});
}

AJS.toInit(function ($) {

  reverseCommentOrder();

  $('#comments-section-title').append(
'<a id="page-comments-reverse" href="#">(Reverse Order)</a>'
); $('#page-comments-reverse')
.css({ 'color':'#999','font-size':'0.65em'})
.click(function (e) { reverseCommentOrder(); e.preventDefault(); }); });

 

 

Like # people like this

This doesn't change the ordering in any nested comments -- I'm not really sure what the correct behaviour would be there. Thoughts?

Actually it sorta makes sense this way, the top level comments are presented in revese order and the replies are nested in reply order under the original - this seems pretty clear to me, I don't think I'd want the replies reversed as well.

As I suspected. I couldn't really figure out what else would work.

This works great for the first comment, but what about the subcomments underneath if people reply?

Hi, 

How can I get to the area Confluence Admin | Look & Feel | Custom HTML???

Thanks in advance

Sibel

Click on the admin cog, then look for look and feel, and custom html in the admin menu.

Like Sibel Yasar likes this

For the sort order, anyway I can do that for only 1 space?

 

Thanks,

 

V

Like Sibel Yasar likes this

Sure, try this:

<script>

var mySpaceKey = 'EXAMPLE'; // Change this to the space key for the 1 space
AJS.toInit(function(){
if (AJS.params.spaceKey == mySpaceKey) { $comments = AJS.$('#page-comments'); $comments.children().each(function(i,li){$comments.prepend(li)});
} }); </script>
Like Sibel Yasar likes this

Thank you David!

If you want to do this per space simply wrap it in {html} tags and place it in the custom header area: Space Admin -> Look And Feel -> Themes -> Configure Theme -> Header.

{html}
<script>
  AJS.toInit(function(){
    $comments = AJS.$('#page-comments'); 
    $comments.children().each(function(i,li){$comments.prepend(li)})
  });
</script>
{html}

Thank you Ronald !

Indeed, the above solution only sorts the top level comments, not the sub comments. The reason is that only the top comment container has an identifier ("page-comments"). Such a global identifier can only be assigned to something that is always present. That is not the case for the sub-comments. So the sub-comments IDs are created on the fly and a solution like the above cannot know them beforehand of course.

Also, inserting the toggle link into the comment block has the flaw that it is removed when Confluence re-creates it after a new comment is added. Only a page refresh will make it appear again.

Other than that it works. Here is a solution how you can do this with a user macro so you can apply it to certain pages only by adding it onto the page.

## Macro title: Reverse Comments
## Macro body processing: No macro body
##
## Created by: George Lewe
## Created on: 2015-11-06
##
## This macro toggles page comments sort order
##
## @noparams
<script>
function rev(i, el) {
   $comments = AJS.$('#page-comments');
   $comments.prepend(el)
}
function reverseCommentOrder() {
   if (AJS.$('meta[name=ajs-form-name]').attr("content") != "editcommentform"){
      $comments = AJS.$('#page-comments');
      $comments.children().each(rev)
   }
}
AJS.toInit(function() {
   if($("#page-comments-reverse").length == 0) {
      AJS.$('#comments-section-title').append('<a id="page-comments-reverse" href="#" onclick="reverseCommentOrder();" style="color: #999; font-size: 0.65em;">[Toggle order...]</a>;');
   }
});
</script>

where do would I insert this in a Space?

I am using 5.7.1.

Thanks!

 

The solution above is a page macro. It only applies to the page when you insert it. It is not space-wide.

 I have placed the above code in the template of my user macro but when I save the page where the macro is it spits out the code and thus not ordering the comments?

How can I make the code be executed and not spit on the page?

Did you figure this out?

Hi all,

I noticed that the editor here replace < with &lt, and > with &gt; in the code I provided. I edited the code section accordingly.

We still use that macro successfully. The user macro is set to "No macro body".

Best regards,
George

This solution will handle new comments and replies to those comments

&lt;script&gt;
AJS.toInit(function(){
   if (AJS.$('meta[name=ajs-form-name]').attr("content") != "editcommentform" &amp;&amp; AJS.$('meta[name=ajs-form-name]').attr("content") != "threadedcommentform"){
      // Only reverse comments if you're not editing one of them
      $comments = AJS.$('#page-comments');
      $comments.children().each(function(i,li){$comments.prepend(li)});
    }
});
&lt;/script&gt;

Hi, I am using confluence V3.5 .

I tried to use David's solution which worked great but now it seems that

I am unable to reply on an existing comment using rich text editor, only by wiki markup (test on Chrome).

did someone else had this issue? is there a solution for this?

I've not tried this in Confluence 3.5 only 4.x. Sorry.

Try this to fix the comment edit/reply problem...

<script>
  AJS.toInit(function ($) {
    if ($('meta[name=ajs-form-name]').attr("content") != "editcommentform"){
      // Only reverse comments if you're not editing one of them
      $comments = $('#page-comments');
      $comments.children()
.each(function (i, li) {
$comments.prepend(li);
}); } }); </script>

 

 

This is excellent David. Seems to work.

So the complete script is:

&lt;script&gt;
function reverseCommentOrder(){
    if (AJS.$('meta[name=ajs-form-name]').attr("content") != "editcommentform"){
      // Only reverse comments if you're not editing one of them
      $comments = AJS.$('#page-comments');
      $comments.children().each(function(i,li){$comments.prepend(li)});
    }
}

AJS.toInit(function(){

  reverseCommentOrder();

  AJS.$('#comments-section-title').append('&lt;a id="page-comments-reverse" href="#"&gt;(Reverse Order)&lt;/a&gt;');
  
  AJS.$('#page-comments-reverse').css({ 'color':'#999','font-size':'0.65em'}).click(function(e){
    reverseCommentOrder();
    e.preventDefault();
  });
});
&lt;/script&gt;

Has anyone attempted something similar with Questions? A reverse sort or a way of maintaining Question order is what I'm looking for.

Is there a way to just add the reverse link without changing the default order? 

I am having the same issue with the Documentation theme in Confluence 4.1.9. I cannot edit comments. It brings up the editor, does not show the existing comment an does not give you a cursor. The preview button does not work either.

This still has issues when you try and reply to a comment. Can anyone help?

Is there an issue to have Atlassian make this part of the code functionality?

Thanks

This works great for the first comment, but what about the subcomments underneath if people reply?

Is it possible to reverse the comments order in the On Demand version of Confluence? We have 145 comments On one of our meeting notes page that grows daily… Scrolling to the bottom is becoming a bit of a pain. Thanks, Robert

Hi Robert,

 

Any update on this issue . Please let us know, we are also searching for the same

No, Karthik.

We have found no solution for reversing the sort order in the On Demand/cloud version of confluence. This is just one of the many limitations that we found since moving to the cloud instance. Seems like it should be a relatively minor thing to fix. Apparently, it's not high on the Atlassian scorecard.

Hi Robert,

But we need to get by adding java script on header side, I think It is also not work out. We are finding any changes. Please suggest if you have any alternate method.

Thanks

There is no way to do it on Cloud.  AS you say, you need to change or add some javascript.  Which you can't do on Cloud.

Can I do that at just the Space Level, not a a global change?

Using v 5.7.1 Space Admin -> Look And Feel -> Themes -> Configure Theme -> Header does not exist.  I get the option of current theme or choose new theme and confirm.

I notice there is a Blog Layout in the Space Tool->Look and Feel->Blog Post Layout

Guess I can just put that in there?

I'm not sure if this thread is still alive. I tried all the codes above, they all work. The thing is, everytime i enter a new comment, it is still displayed at the bottom. Any possible way that the newly entered comment(without refreshing the page) can appear on top as well?

Is this still viable for v 6.15.9 and above?

The Custom HTML inserted via the Admin console - does not seem to be working, but per space it does!

Try now. I've just updated my old answers from 2012 - when Atlassian migrated the content from the old community (answers.atlassian.com), some html was mistakenly escaped.

Thanks David!  

Really appreciate that!

 

Is that the same for the one with the choice of sort order?  add <script> tags?

 

V

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Confluence

Table Row Highlighter - CSS Hack

(Said in an infomercial voice) Has this ever happened to you? You are on a Zoom/Teams call, looking at a large table, and people are confused where on the table you are pointing (insert video of real...

159 views 0 2
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you