Things I Hate #2 — Fake Links
Published: 2021-08-20 14:12:35

---
title: Things I Hate #2 — Fake Links
published: true
date: 2021-08-19 00:35:00 UTC
tags: 
canonical_url: https://kevincox.ca/2021/08/18/fake-links/
---

<main><p>It is a common frustration to be using a website and encounter a link-like UI element that isn’t a real <code>&lt;a&gt;</code> (anchor) tag. Instead it is a <code>&lt;div&gt;</code> or a <code>&lt;span&gt;</code> that the developer has attached an <code>onclick</code> listener to. They have poorly reimplemented the most fundamental UI element on the web.</p>
<h2 id="what-is-wrong"><a href="https://kevincox.ca/2021/08/18/fake-links/#what-is-wrong">What is wrong?</a></h2>
<p>It takes you to the target when you click on it, they were even kind enough to underline it so that you recognize it as a link! What more could I possibly want‽</p>
<p>What is wrong is that <code>&lt;a&gt;</code> is a subtly rich tag with a plethora of built-in features. It is more than a click-sensitive element with some styling. For example here is list of some <code>&lt;a&gt;</code> tag features that I use.</p>
<ol>
<li>Middle-click to open in new tab. (Some people use Ctrl-Click as well!)</li>
<li>Find-in-page and navigate by pressing enter.</li>
<li>Right-click to open in new window.</li>
<li>Send link to another device.</li>
<li>Share link with my friends.</li>
<li>Hover to see where the link is going.</li>
<li>Long press a link to bookmark it.</li>
<li>Check the colour of a link to see if I have been there before.</li>
<li>Use a shortcut to quickly filter through and activate links on the page.</li>
</ol>
<p>And don’t forget that accessibility tools have excellent support for anchor tags. While you can mark up your fake link with a plethora of ARIA information it will only ever approach the accessibility of a native <code>&lt;a&gt;</code> tag.</p>
<p>My point isn’t that any one of these features can’t be implemented on your fake link. The problem is that there is an unknowable set of features that browsers (and other User Agents) implement for links. It is fundamentally impossible to implement them all! Not only because you can’t know all features that exist currently or may be added in the future but also because some features implemented by different browsers are mutually incompatible!</p>
<h2 id="what-to-do-instead"><a href="https://kevincox.ca/2021/08/18/fake-links/#what-to-do-instead">What to do instead?</a></h2>
<p>Just use an <code>&lt;a&gt;</code>! (And of course make sure that the <code>href</code> attribute points to a URL that actually works!)</p>
<p>There is a common use case that requires a bit of work. If you want to handle internal navigation yourself (this can potentially be faster than doing a full-page reload, especially if your site is very heavyweight). Then you should still use an <code>&lt;a&gt;</code> tag, but you will require a bit of JS to disable the default behaviour for regular navigation.</p>
<p>Unfortunately there is no perfect solution here, I wish there was an <code>onfollow</code> event that we could hook for when a user activates a link in the “normal” way. Unfortunately semantic events are not present the web so we are left with the following:</p>
<div><pre><code><span>mylink</span><span>.</span><span>addEventListener</span><span>(</span><span>"</span><span>click</span><span>"</span><span>,</span> <span>e</span> <span>=&gt;</span> <span>{</span>
	<span>if</span> <span>(</span><span>e</span><span>.</span><span>altKey</span> <span>||</span> <span>e</span><span>.</span><span>ctrlKey</span> <span>||</span> <span>e</span><span>.</span><span>shiftKey</span><span>)</span> <span>return</span><span>;</span>

	<span>e</span><span>.</span><span>preventDefault</span><span>();</span>

	<span>// Do your link handling here.</span>
<span>});</span>
</code></pre></div>
<p>Logically it is quite simple, ignore the event if any modifier keys are held (these are used by many browsers to open in new tabs or windows, which we will let the browser handle).</p>
<p>This even works for keyboard-based activation as that triggers a synthetic <code>click</code> event!</p>
<p>I don’t like that I need to hardcode this logic into my app, I would love to see a <code>follow</code> or <code>activate</code> event for buttons and links however this is the best solution I am currently aware of. If you have a better solution please let me know!</p>