| Project: | Drupal core |
| Version: | 7.x-dev |
| Component: | comment.module |
| Category: | task |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | needs review |
Issue Summary
(Not to be mixed up with Creedence Clearwater Revival :-)
I'm proposing this for 7.x-dev, because it is closely related to issue #26966: Fix comment links when paging is used., which has recently been closed with success, and because it is a very small change that does hardly interfere with existing code. If not that, let it go for 8.x-dev.
The Problem
Comment links used to look like this:
/node/17#comment-42
Let's call this the classic comment link. In general it does not work—never did, except if the comment happened to be on the very first page of a node display.
Drupal 7 has, thankfully, been fixed, however, the fix works only with new-format comment links that look like this:
/node/17?comment=42#comment-42
The reason for this, so I've been told, is that Drupal on the server (i.e. PHP in general) has no way of reading the hash part of the URL, so it has to be duplicated in the query/search part.
Apart from being wordy, redundant, and ugly, this has a rather obvious problem: All preexisting classic links all over the world still don't work properly and never will, unless we do something about it.
Proposal
Since I would consider that nearly a show-stopper, I have thought about it for a while and already made a vague proposal in the issue thread mentioned at the top. Currently I consider the following to be the best solution.
Add a bit of JavaScript code to each paged node (or to every node display), that looks over the page for classic comment links and changes them to new-format links on the fly. This can very efficiently be done thanks to the document.anchors property of the DOM (Document Object Model), which hands over a collection of all links in the document on a silver platter.
All the JavaScript code has to do is check each anchor/link for the classic comment link format (a one-liner with one regexp) and, if it is one, change it to the new format. This would be invisible to the user and cause no visible change in the displayed page. Only if the user's mouse pointer hovers over a link, the browsers status line now shows a new-format link and uses it, rather than the original classic link, if the user clicks on it.
Limitations
Obviously this works only for links that are on Drupal pages, not for links in other web sites, but for that there is an additional proposal (see below). I believe that, fortunately, most links to Drupal comments are on Drupal sites, most of them probably being links from one comment to another on the same site.
The other limitation is that it requires JavaScript to be enabled in the user's browser. I am not sure about the general Drupal stance on this, but Drupal already contains loads of JavaScript, and browsers with JavaScript blocked are exceedingly rare these days. They would just see what they always saw.
Add it to Drupal
I am not a Drupal developer and am unfamiliar with the procedures, so it would be nice if one of the developers could incorporate the little bit of JavaScript, probably less than 10 lines, in one of the existing .js files, ideally adding it to some code that already runs after the page is loaded (when the DOM is completely loaded). Perhaps one of the people who worked on the original issue mentioned at the top could do that.
If somebody came forward to do it, I would gladly write the JavaScript code, if I'm assured that the work will likely not be in vain.
Additional Proposal Regarding Foreign Links
A simplified version of the working code for issue #677082: Comment links - JavaScript solution could be used to allow foreign links, i.e. classic comment links on non-Drupal pages to work. This would work as follows.
- User clicks on classic comment link on a non-Drupal web page.
- The Drupal server cannot process this link properly and opens the very first page.
- Once the page is open, the JavaScript code checks the link and discovers that it is a classic comment link.
- The JavaScript program looks at the hash part of the URL (yes, JavaScript can do that) and checks whether the targetted comment is indeed on the current page.
- If not, the program reformats the link to the new format and calls up the page again, this time with the proper comment query included.
- Now that Drupal learns about the desired comment, it opens the proper page.
This requires two page calls in the hopefully infrequent case that the desired comment happens not to be on the first page.
Additional Proposal Regarding Near-Classic Links
An odd recognition on the side is that we could use much simpler, nicer links that look almost like the classic ones:
/node/17?comment=42
An additional, even smaller piece of JavaScript would have to look at the URL after the page is loaded and jump down to the desired comment, just as the browser does on its own when presented with the hash part, #comment-42. Very similar code already exists in issue #677082: Comment links - JavaScript solution, so there wouldn't even be much work to do. I think it can't hurt to add that bit of code as well. Perhaps in a few years that could become the most favored comment link format.
Nothing would preclude the continued use of the additional, redundant hash part, so these formats could exist side by side.
Comments
#1
#677082: Comment links - JavaScript solution has been marked as duplicate, contained a link to example code.
#2
#3
Great! Thanks. And it's such a compact piece of JavaScript code.
Good job! Sometimes JavaScript can help to solve the trickiest problems.
I'll bite my tongue and swallow my comment on jQuery use (:-). But the thing has already hooked on to Drupal anyway.