While redoing Where and how to start learning Perl I had some difficulty finding out to link to a named anchor in another node on the Perl Monks site, for instance to Regular Expressions in Tutorials.

I’ve got it now and I wonder if this link works in every browser. I’ve tried Mozilla 1.5a and IE 6.0 and it seems to work fine.

I realize that in the majority of cases, such links are not necessary or disturbing. But sometimes it is just nice to use a named anchor anyway.

Link to a named anchor in the same node
In a long node it might aid the reader to make chapters, or just refer to some specific part in the node.
With links like these: Anchors like these can be included in the section where the link should point to:
<a href=#anchorname>Text Text Text</a>
<a href="#anchorname">Text Text Text</a>
<a name=anchorname>Text text text</a>
<a name="anchorname">Text text text</a>

Link to a named anchor in another node
A lot of ways to link to a named anchor in another node do not work correctly, the link will be translated into a faulty URL. Some examples that do not work:
[nodename#anchorname|Text text text]
 [id://954#anchorname|Text text text]
<a href="nodename#anchorname">Text text text</a>
<a href=nodename#anchorname>Text text text</a>

The solution of a link with a named anchor seems not to be possible with square brackets […]. The only way seems to be to use the “real HTML” <a href=…> with the full path from the root. These examples work fine:
<a href="/index.pl?node=954#regexdata">Regular Expressions</a>
<a href=/index.pl?node=954#regexdata>Regular Expressions</a>

Are there easier and/or more elegant ways to do this? Am I missing something?

Replies are listed 'Best First'.
Re: Named anchors in current node or in another node
by castaway (Parson) on Aug 18, 2003 at 12:15 UTC

    You're right, the best way to do that is with an <a> tag. There's been plans to fix this for a fair bit; you can see the work-in-progress at tye&nbsp;. BTW, you can leave out the index.pl part of the URL (just /?node=...), you should use node_id= instead of node= (saves some DB server CPU, which is in short supply), and you should always quote.

    PS -- This is really theorbtwo, I'm just too lazy to log in as me.

      Again, please leave off the "/" as well. PM itself will start doing this at some point and then we may have URLs that control presentation like http://perlmonks.org/robot/ but the "/" in the internal link would remove such when you follow it.

                      - tye
Re: Named anchors in current node or in another node
by dbwiz (Curate) on Aug 18, 2003 at 12:33 UTC

    See jeffa's home node for some advice on the same subject.

Re: Named anchors in current node or in another node
by woolfy (Chaplain) on Aug 18, 2003 at 19:18 UTC
    Update: My, what was I thinking...

    Use the version of the internal and external links with "quotes"!

    With links like these: Anchors like these can be included in the section where the link should point to:
    <a href=#anchorname>Text Text Text</a>
    <a href="#anchorname">Text Text Text</a>
    <a name=anchorname>Text text text</a>
    <a name="anchorname">Text text text</a>

    Avoid using /index.pl, since that would stress the servers of the Perl Monks website too much, now or in the near future.

    The most elegant solution so far to link to a named anchor in another node is this:
    <a href="?node=954#regexdata">Regular Expressions</a>

    Thanks to the high class (and serious) info from castaway and tye. Please prevent people like me to cause problems with the server (now or in the future).