It can be very helpful, especially for longer documents, to use scripting so that an HTML page automatically generates the content of a link that points to a heading, a figure, or a table. For example, the script might pull the actual heading text into the link, and if the headings are automatically numbered using CSS, may also pull in the number of the heading. As the document evolves, and the heading text changes, or its position in the document changes, there is no need to update all the links pointing to that heading.
If you are linking to headings and figures for a page that will be translated into another language, or for a multilingual page, this article looks at things you need to bear in mind and provides markup templates that will be helpful.
We won't provide script code to do the job in this article, but will focus instead on the markup you should aim to produce. Most of the examples of markup in the article represent the outcome of the link generation process, rather than the markup that the content author actually uses.
Here we summarise the bare bones of the solution. Read the rest of the article for more detail.
The markup used by the content author can vary, but should allow them to easily see what the link will eventually point to. The following is just an example.
This article focuses on the outcome of the link generation process, rather than the markup that the content author actually uses.
Links to unnumbered headings would typically benefit from a structure such as the following.
However, for numbered headings, the number should be isolated from the rest of the heading text, and the
dir attributes should be applied to the latter, as in this example.
For the link text, include all the markup inside the heading tag, not just the characters.
Also identify the type of link using semantic class names, so that, for example, links to headings can be styled differently from links to figures.
lang attribute indicates the computed language of the heading itself.
dir attribute indicates the computed direction of the heading itself.
If your code generates text, such as "Fig." to be included in the link text, ensure that it is easily localised for translated content. Also, don't assume that spacing and punctuation will be the same for all languages: ensure that the punctuation and spacing are part of the localised string. For example, a link to Fig. 12 would become 図12 in Japanese.
The rest of the article provides additional details.
Make sure that you extract the markup inside the heading, rather than just the characters.
.textContent to access the text of a heading. Instead use a method that copies all the nodes inside the heading tag.
Apart from the fact that it often makes the link text more readable when you include the inline markup that the heading contained, this is particularly important if the heading text contains markup that sets the language or direction of the text it contains.
For example, the following heading uses two important spans. The first makes it possible to apply a font capable of handling the diacritics in the transcription, and the second both supplies language (enabling use of an appropriate Syriac Estrangelo font) and direction information. It's important to carry that markup into the link text.
In some languages is may be desirable to style links containing heading text differently from links containing figure pointers.
For this reason it is useful to distinguish the various different types of link from each other, using class names to indicate the semantics of the markup.
Here's an example of class names that distinguish the figure from the header.
In a multilingual document or a translation of a document it may be necessary to adapt any text generated by your script.
For instance, the example in the previous section generates the text "Fig. " and inserts it into the link text alongside the number of that figure. If the same script is used for a Japanese translation of the document, or is applied to a section of the document that is in Japanese rather than in English, it would be necessary to replace the generated text with "図".
Note, significantly, that no period or space is needed for the Japanese version. It's important not to assume in your script that all languages will use punctuation and spacing in the same way. Those characters should be part of the localised replacement string, rather than added by a separate line of code.
The box below shows what the generated link markup could look like. (Note also that there is no space inserted around the link text "
The following guidelines are particularly important when dealing with a heading in a different language from the paragraph containing the link, but some aspects may also be important in monolingual situations. Including this markup should not cause any problems for the general case.
There are similar considerations for documents containing text that runs in multiple directions; for example, when embedding Arabic, Hebrew, Dhivehi, etc text into a Latin context. It is essential to apply the correct base direction for a sequence of characters that is inserted into a paragraph, and this requires finding the computed direction of the markup around the original heading.
For example, take a heading such as:
In this case the direction is declared on the heading tag itself, but it could be declared on an element further up the hierarchy. This code displays as shown just below. Note the location of the text "W3C".
Drop just the inner HTML of the heading tag into our previous paragraph, and you get the "W3C" on the wrong side of the Arabic text
If, on the other hand, we apply a
dir attribute with the appropriate direction value around the inserted text ...
... then this produces the expected result:
In bidirectional text, it generally makes sense to isolate strings or markup fragments that are dropped into a location in an HTML file, so that the text inside the string doesn't interact directionally with text outside it. In HTML5 the
dir attribute isolates the content of the tag it is attached to from the surrounding text, so we have been isolating the reference from the surrounding paragraph already.
However, let's suppose that we want the reference link to include the number of the heading, as well as the heading text.
Suppose our heading looks like this in its original RTL context.
We would expect to see something like this in an English paragraph:
Unfortunately, if we simply follow the advice given so far (to put
dir on the anchor tag), we would see the following:
The "W3C" is in the right place, but the section number looks wrong. To make this work as expected, we need to also isolate the section number from the heading text, and add the directional information to the heading text, rather than to the anchor tag as a whole. The following code would do the job.
You could also, if you wanted, move the "2" out of the link altogether, like this:
This approach would still produce expected results if the text was dropped into a RTL paragraph. In that case we do want the section number to appear to the right. There is no need to change the markup for this to happen.
It also works with alphabetic section numbers.
auto value of the
dir attribute looks at the incoming data, finds the first strong directional character (in the examples just above, an Arabic letter), and sets the base direction for the contents of the anchor tag to that direction (for the examples above, RTL).
If, however, the heading begins with ASCII characters (on the right), like this ...
... we have a problem.
Because the first strong bidirectional character happens to be "H" in this case, the base direction for the inserted text is set to LTR.
In summary, since first-strong heuristics can be fooled, it's better to ascertain the base direction of the heading by looking at the DOM, and apply that to the embedded link.