15

Sometimes I'd like to link to another answer of the same question in my own answer. The simple way to do it is to copy the permalink of the answer and use that. However, that is likely to reload the page when clicking the link because SO links are not normalized to a canonical form. I also tried making a link of the form [link](#id) but this won't render.

I'd prefer a technique that avoids reloading the page, if possible.

(EDIT: If there's no better way of doing this, of course I'll accept that as an answer too. I'd just like to make sure I didn't miss something.)

8
  • 1
    Is this a feature request then?
    – Oded
    Nov 29, 2013 at 8:36
  • It is ... ? @Oded done :D
    – l2aelba
    Nov 29, 2013 at 8:39
  • @Oded I wasn't sure if it is possible and I just don't know how, or is it not possible yet. I guess OP might have similar doubt... so if you could be so nice and confirm it's not implemented at the moment?
    – Mołot
    Nov 29, 2013 at 8:53
  • 1
    This is not currently supported. We require an http to recognize a link as such.
    – Oded
    Nov 29, 2013 at 9:05
  • AFAIK, we have to open a new browser tab with the shortlink, and this will produce the full anchored URL.
    – brasofilo
    Nov 29, 2013 at 11:57
  • 7
    Note that another answer of the same question does not necessary be on the same page.
    – Alvin Wong
    Nov 29, 2013 at 15:16
  • I got the feeling they removed my earlier comment from here?
    – n611x007
    Jan 19, 2017 at 2:15
  • 1
    This is not actually a feature request; nothing is being requested. It's just a support question with an incorrect tag, perhaps because the user might've thought the feature didn't exist, but well, it still doesn't make any such feature request. Nov 20, 2023 at 17:37

3 Answers 3

11

[link](#id) used to be supported and was nice in most cases, but it became unreliable for those rare questions which span multiple pages. I prefer to accept the reload and use the normal [link](/a/id), as it's the most convenient and unambiguous way of reliably specifying the target.

2
  • I was not aware that questions can be several pages long. So I won't try to use only the ID then. But do you mean that [link](#id) works for you? (It doesn't render as a link for me.)
    – Szabolcs
    Jan 9, 2012 at 13:16
  • 2
    @Szabolcs No, it does not. It worked in the past but was disabled at some point. I forgot about the change when I first wrote my post.
    – Jeremy
    Jan 9, 2012 at 13:19
21

You can't do a direct anchor link. Do what you're already doing and use the share link you get beneath their post:

click the "share" button and copy the URL that pops up

This works just fine and is resilient to scenarios which can make a simple #anchor link fail (question migration, the question has 30+ answers and is now paginated with the answer you're linking to on a different page, or someone views your post revision independently). Plus, using the share URL can get you the announcer, booster and publicist badges.


There is one way to create an anchor link that involves no page reload. This method is unreliable and broken by basic site interactions; I don't recommend using it ever and I'm only describing it for comprehensiveness. Use the share link.

Take the full URL of the question and strip it down to a domain-relative link (e.g. for this question, /questions/209342/how-to-link-as-an-anchor-link-to-another-answer-in-same-page). Append the anchor ID onto that manually (#209580 for this answer) to get a URL that jumps to an answer without reloading the page: here it is in action. This also accounts for the popular HTTPS Everywhere addon, and site migrations.

This method only saves a page load if I'm already on the exact URL you're assuming. That means this method is broken by title changes, by a trailing slash, by visiting this page via an answer share link or after editing an answer (appends a /post_id to the URL), or via a notification (appends ?noredirect=1). It also breaks of there's >30 answers and thus the question is paginated, and my current sort mode (active/oldest/votes) pushes that answer onto another page.

You avoid these breakages by keeping up with all title changes, ensuring everyone is on the same URL first (give them a master link to click), and working around the 30+ answers issue somehow (ask everyone to use the right sorting method?).

I recommend this trouble is not worthwhile for avoiding something as trivial as a page reload, and that you just use the share link.

6
  • 4
    @Marc -1 for non free hand circles! Dec 2, 2013 at 8:30
  • 2
    @ShaWizDowArd I use a trackball with my off-hand; I'm not sure I'm up to circles... Dec 2, 2013 at 9:09
  • @ShadowWizard Three years and a month later, and freehand circling has been delivered. :) Jan 17, 2017 at 17:01
  • @doppelgreener heh, about time! ;) Jan 17, 2017 at 17:27
  • 1
    This answer didn't contain the second (highly inadvisable) part until today, but it did still say just use the share link, which is still the only worthwhile way to do this. Jan 17, 2017 at 20:35
  • Self-plug: a userscript can skip a page reload when clicking an answer link in the recommended form. Which I guess only reinforces the advice to avoid the latter form. Nov 20, 2023 at 10:04
2

Use the /a/answerID link you get by clicking the “share” button under the answer. This is the only kind of link that will work in all circumstances, such a when viewing a single post’s revision, viewing a post in a review queue, or when the answer list is paginated, when an answer is moved between questions on the same site (which does happen, if rarely), etc.

If you want to prevent page reloading when following answer links, I recommend a user script of my own creation, which reroutes full answer links, no matter their form, to anchor links on the current page whenever it can. If the answer is not detected on the current page, the link will be followed as usual. (I guess you could say this should be built-in to Stack Exchange, but in the meantime, a user script should do.)

6
  • "This is the only kind of link that will work in all circumstances" <-- that's not accurate, both that there are other URLs that work and domain-relative links of that sort won't work in all circumstances. At least both the full canonical link, including both the question and answer IDs, such as https://meta.stackexchange.com/questions/118381/what-is-the-proper-way-to-link-to-another-answer-on-the-same-question/394822#394822 for this answer, and https://meta.stackexchange.com/q/answerID (using the answer's ID in the format intended for question IDs) will also work in all circumstances.
    – Makyen
    Nov 20, 2023 at 17:17
  • Including the domain name is required for it to work in all circumstances, as the HTML text may be transcluded onto a different site and/or obtained via the SE API and displayed elsewhere. I've had to write code which fixed such domain-relative links in posts, so the links would work as intended, for the FIRE userscript and post previews on metasmoke. I should probably also say that such code is always going to be needed, as SE's code generates domain-relative links in the HTML under some circumstances, so this concern is largely a semantic issue with saying "work in all circumstances".
    – Makyen
    Nov 20, 2023 at 17:17
  • Full /questions/ link doesn’t work after an answer is moved; /q/ANSWERID does, but… why would you do that? And third parties misusing HTML is not a case users should be concerned about; that’s the third party’s problem. Nov 20, 2023 at 18:20
  • Moving an answer in a way that keeps the /a/answerID link valid only happens when the question is merged with another question, which is very rare and something the author of the answer should be able to become aware of, allowing them to edit their answer to change the link shortly after the merger happens. When the question is migrated to another site, the answer should get a new answer ID, which should break all links. The cases where links such as you propose don't work are transparent to the author, so they can't mitigate the issue. Overall, what link to use is a trade-off.
    – Makyen
    Nov 20, 2023 at 18:57
  • My objection is that rather than presenting the various choices to the user along with the trade-offs inherent in each of the choices (none of which are perfect), you are presenting one single pre-chosen option, which does have both positives and negatives, which you are promulgating without presenting both the positives and negatives and while making at least one factually inaccurate statement in support of your position.
    – Makyen
    Nov 20, 2023 at 18:58
  • Note: I don't disagree that the /a/answerID based URL is a good one to choose under many circumstances. But, if it's the right one to choose depends on what the author wants. It's better to provide options and the positives and negatives of each choice, so the author of the post that will contain the URL can make the choice which works for what they are wanting to do.
    – Makyen
    Nov 20, 2023 at 19:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .