This continues the JavaScript solution tail of #26966. Here is the information in brief, so you don't have to go back to that issue, unless you want to know the last detail.
The problem on hand is that comment links like http://winhlp.com/node/10#comment-304 do not work on a multi-page node display in any version of Drupal, including version 7, unless the comment is on the first page.
A different link format, repeating the comment number to the left of the hash, has been devised for version 7, but will apparently not be backported. It is also incompatible with all pre-existing comment links in the classic format, as shown above. (That could be helped with another, much simpler little piece of JavaScript, yet to be devised.)
I have created a complete JavaScript solution that can be used as a stopgap measure for all Drupal versions up to 6.x. To install it, you have to make sure the following line of code is embedded on node display pages. (It does not hurt on any other page either.)
<script type="text/javascript" src="http://winhlp.com/commentlink.js"></script>
That's all. You can now reach every comment of that article with the simple, classic link ending in, for example: #comment-42
For a test you can add the code just to one article. Make sure the input format allows HTML code.
You can add it to the theme, but since it works in all themes, it should ultimately be inserted in includes\common.inc, along with jquery.js and drupal.js. (Search the file for the reference to either of these.) But if you add it to the theme, it should ideally be added as a line like this to the theme's .info file:
scripts[] = commentlink.js
Then you have to download commentlink.js and copy it into the theme folder. But, as I said, ultimately it does not belong in the theme.
If you embed this in a theme or patch it into the Drupal templates, so it works for all articles, be aware that the code has not been widely tested. If you discover an error, please let me know.
In the special case that you want to embed a link to a comment in the main article itself, the one to which the comment belongs, then you have to modify the link a bit, because the normal hash link on the same page would not reload the page and would thus not activate the program. Inserting a question mark before the hash is enough to force a page reload and start the program. Thus, if you want to link to a comment directly from its parent article:
Wrong: <a href="/node/10#comment-304">link text</a>
Right: <a href="/node/10?#comment-304">link text</a>
I guess there are many other ways to modify the URL sufficiently, but this seems simple enough.
The readable, commented source code is available at: http://winhlp.com/commentlink-source.js
It seems simple enough and can probably be backported most easily to any older version of Drupal that supports this comment link format.
Known problems:
- The program pages forward linearly and thus increases the server load and the waiting time for the end user.
- The program increases the article read counter, because it actually reads several pages, and thus overstates the popularity of linked pages (somewhat like the SEO fraudsters :-).
- The back button of the browser does not work on pages reached through this kind of paging.
Short explanation of the algorithm:
- The javascript code hooks into the window.onload event (while keeping other hooks alive that other programs may have put there).
- After the page is fully loaded, it checks whether the URL ends in #comment-, followed by a comment number. If not, it finishes immediately.
- The program checks whether the desired comment is on this page. If so, it scrolls down to the comment and finishes.
- The program checks the URL for a cs parameter that would be put there by itself after leaving the preceding page. The cs parameter bears the number of the first comment on the previously opened, i.e. the preceding page.
- The program checks whether that same comment can still be found on the current page. If so, it assumes that this is the same, last page, gives up and finishes. This is because Drupal has the unpleasant habit of showing the last page when the next page number, or any excessive page number, is given in the URL search parameter.
- The program reads the page number from the page= search parameter in the URL.
- The program searches the current page for comment anchors (id="comment-N", where N is a comment number) and memorizes the first comment number it finds.
- The program builds a new URL that ends with:
?page=[current page number + 1]&cs=[first comment number on current page]#comment-[desired comment number]The expressions in brackets represent simple numbers.
- 9.The program calls up the thus constructed URL, which calls up the program again and starts all over from step 1.
If it is excessive (currently set to 99 for testing, could be increased to 999 later) it finishes. This is merely an expression of endless loop paranoia. (:-) Such an endless loop could happen, for example, if somebody made a programming error elsewhere, such that the paging algorithm became unstable and the same page would not contain exactly the same comments every time it is opened.
Comments
Comment #1
hgmichna commentedA little addition—after you put the line:
scripts[] = commentlink.jsinto your theme's .info file and put the file commentlink.js into your theme's main folder, then you have to visit, merely to open, the theme settings to have it picked up by Drupal and put in its cache.
Who has tried this solution? Please report. Have you seen any problems?
I have installed it on all of my 5 Drupal 6.15 sites. I have not seen any problem so far. On my sites links to very old articles are not frequently used, which makes the paging unproblematic. I also don't have articles that have more than 3 or 4 pages, so again no problem.
What are your largest numbers of pages for one article? Have you checked or can you estimate how many links there are and how often they are used? My guess is still that on most normal Drupal sites there are no significant problems with this solution at all.
Comment #2
sunThanks for taking the time to report this issue.
However, marking as duplicate of #679772: Old-school comment URL #fragments still don't display correct page.
You can follow up on that issue to track its status instead. If any information from this issue is missing in the other issue, please make sure you provide it over there.