created: 20190311094603013 modified: 20201130064330388 tags: [[HTML in WikiText]] title: Anchor Links using HTML >> * >>What do they do? * >>How do I make them? * >>Limitations and things to look out for * >>More examples * >>Suggestions for HTML elements to use * >>Suggestions for target names to use * >>Avoiding duplicate IDs when transcluding * >>Style Hints >> !! What do they do? In TiddlyWiki anchor links can help us link to target points and distinct sections within rendered tiddlers. They can help the reader navigate longer tiddler content. For example this link, >>Bottom, should take you to the bottom of this tiddler. The "Back to the top" link below should return you back to the top of this tiddler. >>Back to the top >> !! How do I make them? There are only 2 steps to making anchor links. * First, create the target point (called an 'anchor') where you want it. * Second, add a link to it where the reader might need it. That's it. ''Step 1: Create an 'anchor' (link target)'' ``` ``` *Add an <<.attr id>> attribute to any HTML5 tag and place a single `#` before the 'address' value. >>Here are some suggestions of tags you can use. * Place this code at the target location. E.g. at the bottom of the tiddler. ''Step 2: Create a link to the target anchor'' ``` Bottom ``` *Create an `` link using exactly the same 'address' value as the target but with double `##` characters preceding it. *Place this code where it will most help the reader. The above example can be adapted to work for many situations in ~TiddlyWiki, such as the table of contents at the top of this tiddler. ''Is it really that simple? '' Yes, it's that simple. BUT... in ~TiddlyWiki there are some small differences from standard HTML5 anchor links and some specific ~TiddlyWiki limitations. >>Back to the top

>>Limitations and things to watch out for

''Step 1:'' Creating the target anchor * Unlike standard HTML5, in ~TiddlyWiki you must put a single `#` character in front of the target address value that follows the <<.attr id>> attribute. (Notice the single `#` character used in the #Bottom_of_tiddler in Example 1.) * Unlike standard HTML5, ~TiddlyWiki anchor links are case sensitive. In the example above #Bottom_of_tiddler works, but #bottom_of_Tiddler wouldn't. * Avoid using the % character in your link, as some browsers have a bug where navigation to anchors containing a % character can sometimes fail (see warning below). * >/>Avoid using any whitespace in your link (see > title="click to go to note 1.">note 1 below). * Each link address should be unique in the TiddlyWiki. If transclusions create multiple instances of the same <<.attr id>> attribute, navigation will take place to the first instance found that satisfies the above criteria. To avoid this, you can use the [[qualify macro|qualify Macro]] in both the <<.attr id>> and the <<.attr href>> attributes in a similar style to the example given below: > >Avoiding duplicate IDs when transcluding. ''Step 2:'' Creating the link to the target * Remember that the <<.attr href>> value should start with ''two'' `#` characters, one more than the <<.attr id>> value of the target anchor. * Remember that ~TiddlyWiki anchor links are case sensitive ''Step 3:'' Navigating to the target Normally you can click on the link and ~TiddlyWiki will scroll the window so the target anchor is at the top of the window, but there are several limitations: * Anchor links in ~TiddlyWiki do not work if the tiddler is not in the Story River, or is not displayed in the sidebar. * Anchor links in ~TiddlyWiki do not work if the tidlder is in the Story River or sidebar but anchor marked target point is not visible on the page (e.g., the tiddler is folded or the anchor is within a non-displaying $reveal widget). Ctrl+Click on such an anchor link will open a new browser window/tab loading the base web page and will not navigate to the expected tiddler. * Anchor links in Tiddlywiki do not work if Control Panel > Appearance > Story View is set to Zoomin (the single page view) and the tiddler is not at the top of the story river (i.e. not displayed). * When an anchor link is clicked the browser's navigation history will be updated with the link's href value appended to the web page's URL. This will happen even if the Control Panel > Settings > Navigation History value is set to "Do not update history". <$macrocall $name=.note _=""">>> title="click to return">1. The [ext[HTML5 specification|https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/id]] for the <<.attr id>> attribute says that its value should not contain any whitespace. Currently ^^(Dec 2020)^^ an <<.attr id>> value with whitespace can sometimes work. However, it may not work in all browsers and is not guaranteed to work in the future.""" /> <<.warning "Some browsers may fail to navigate to an anchor target whose ''id'' value includes a `%` character followed by 2 digits. This is a bug in the browser and nothing that ~TiddlyWiki can fix. To be safe, avoid using the `%` character.">> >>Back to the top

>>More examples

In each case the target code is placed at the destination the reader navigates to, the link code is placed where it will help the reader navigate from. ```

My Target Heading

Link to My Target Heading ``` This looks like:

My Target Heading

Link to My Target Heading
```
!! My Target section A `
` HTML tag is generally used to define sections in an HTML document, such as chapters, headers, footers, or any other sections of the document.
Clicking this anchor link goes to: Link to My Target Section ``` This looks like:
!! My Target section A `
` HTML tag is generally used to define sections in an HTML document, such as chapters, headers, footers, or any other sections of the document.
Clicking this anchor link goes to: Link to My Target Section
>>Back to the top

>>''Suggestions for HTML elements to use''

Some suggestions for the HTML element you choose for marking a position using the <<.attr id>> attribute: | ! Element | ! Sample code | ! Observations | |<<.def anchor>> |<$text text=""""""/> |Should work perfectly everywhere| |<<.def heading>> |<$text text="""

A New Heading

"""/>|Headings can have <<.attr id>> attributes as well| |<<.def section>> |<$text text="""
This is a new section
"""/>|If you use the `section` element it should probably have an <<.attr id>> of its own| |<<.def span>> |<$text text="""Marker 007 is here!"""/>|`span` elements can be used to insert an <<.attr id>> mid-paragraph| >>Back to the top

>>Suggestions for target names to use

| ! | ! Sample code | ! Observations | |<<.def Recommended>> |<$text text=""""""/> |Include the tiddler name for easy unique <<.attr id>>s| |<<.def "Avoid: whitespace">> |<$text text="""

A New Heading

"""/>|Whitespace is not guaranteed to work in all browsers| |<<.def "Avoid: %">> |<$text text="""
This is a new section
"""/>|URI encoding (turning space into %20 and so on) may not work in some browsers| |<<.def "Avoid: duplicate IDs">> |<$text text="""Marker 007 is here!"""/>|<<.attr id>> values should be unique within entire wiki| |<<.def "Avoid: duplicate IDs">> |<$text text=""""""/>A different marker 007|If <<.attr id>> values are duplicated, one will be ignored| >>Back to the top

>> ''Avoiding duplicate IDs when transcluding''

The <<.attr id>> and anchor link to this header has been made as follows: ``` >>Avoiding duplicate IDs when transcluding

>>Avoiding duplicate IDs when transcluding

``` ;For this to work: * The <<.attr href>> and <<.attr id>> values use the [[macro invocation syntax for attributes|HTML in WikiText]], using the [[qualify Macro]] to create a unique <<.attr id>> value. * Both the <<.attr id>> and the anchor link must be in the same tiddler. * There must not be any quote characters between the `=` and the `<<`. * The double quotes around the target name are optional, but recommended. * There should be a total of three `>` characters after the target name, two `>>` to close the `<` to close the `>>Back to the top

>>''Style Hints''

If you don't want the link to look like an external link, give the `` element's <<.attr class>> attribute the value <<.def tc-tiddlylink>> and if you want the link to also be italic & bold, give the additional classes <<.def "tc-tiddlylink-missing tc-tiddlylink-shadow">> like below: ``` Back to the top or just back to >>Hints. ``` Back to the top or just back to >>Style Hints. If you want to include an <<.attr id>> attribute to a link in your sidebar Table of Contents, you can include it in your listed tiddler's <<.field caption>> field similar to the way shown below: ``` Your caption ``` >> >>Back to the top