{"componentChunkName":"component---src-templates-post-js","path":"/blog/2020/06/03/links/","result":{"data":{"post":{"html":"<p>I see some occasions where web developers who are a bit less familiar with accessibility use links incorrectly in different forms. So I thought let’s write a quick guide on <em>Links</em> in JavaScript apps.</p>\n<!--more-->\n<h2 id=\"background\" style=\"position:relative;\"><a href=\"#background\" aria-label=\"background permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Background</h2>\n<p>It’s safe to assume that anyone who’s used the web have seen them. They take users from one page to another either within the same application or to an external website. There were really hot when they were introduced back in <strong>1966</strong> by a team led by <a href=\"https://en.wikipedia.org/wiki/Douglas_Engelbart\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Douglas Engelbart</a>.</p>\n<p>In the early 1980s <em>Ben Shneiderman</em>, a computer scientist at the University of Maryland, with his graduate student <em>Dan Ostroff</em> were preparing a videodisk exhibit for a museum. It was then where they used a caption that had all the information and users could click on it to see the photo associated with it.</p>\n<p>At first, this discovery was called <code class=\"language-text\">embedded menu</code> but that term was quickly replaced by <code class=\"language-text\">hyperlink</code>. <a href=\"https://en.wikipedia.org/wiki/Tim_Berners-Lee\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Tim Berners-Lee</a> cited Shneiderman’s hyperlink work in his spring 1989 manifesto for what would become the World Wide Web.</p>\n<p>Back then, this feature was super hot and it still is. People use it without even thinking about it and it’s become a natural part of the web.</p>\n<h2 id=\"enough-background-tell-me-whats-this-all-about\" style=\"position:relative;\"><a href=\"#enough-background-tell-me-whats-this-all-about\" aria-label=\"enough background tell me whats this all about permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Enough background, tell me what’s this all about</h2>\n<p>Let’s review what they do:</p>\n<ul>\n<li>As mentioned before they allow users to navigate between pages within a site or to an external one.</li>\n<li>And more importantly they help search engines and bots to find resources and going from one page to another by following the links. Think of this as a giant maze where they need to blindly follow the links to discover stuff.</li>\n<li>And last, the crawlers use them to understand the architecture of a site and the type of content they have. It’s a critical part of their decision making when assigning a page to a specific topic.</li>\n</ul>\n<h2 id=\"but-how-do-you-make-a-link\" style=\"position:relative;\"><a href=\"#but-how-do-you-make-a-link\" aria-label=\"but how do you make a link permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>But how do you make a link?</h2>\n<h3 id=\"code-classlanguage-textacode-tag-with-code-classlanguage-texthrefcode\" style=\"position:relative;\"><a href=\"#code-classlanguage-textacode-tag-with-code-classlanguage-texthrefcode\" aria-label=\"code classlanguage textacode tag with code classlanguage texthrefcode permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><code class=\"language-text\">a</code> tag with <code class=\"language-text\">href</code></h3>\n<p>Turns out it’s not as straightforward as one might think. The simplest way to put a link on your site is to use a good old <code class=\"language-text\">&lt;a></code> tag with a <code class=\"language-text\">URL</code> which it points to in the <code class=\"language-text\">href</code> attribute.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"79777914997817570000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`<a href=&quot;/elsewhere&quot;>A good link, YES ╰(*°▽°*)╯</a>`, `79777914997817570000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"html\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-html line-numbers\"><code class=\"language-html\"><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>a</span> <span class=\"token attr-name\">href</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>/elsewhere<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span>A good link, YES ╰(*°▽°*)╯<span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>a</span><span class=\"token punctuation\">></span></span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span></span></pre></div>\n<h3 id=\"code-classlanguage-textacode-tag-with-code-classlanguage-texthrefcode-and-event-handler\" style=\"position:relative;\"><a href=\"#code-classlanguage-textacode-tag-with-code-classlanguage-texthrefcode-and-event-handler\" aria-label=\"code classlanguage textacode tag with code classlanguage texthrefcode and event handler permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><code class=\"language-text\">a</code> tag with <code class=\"language-text\">href</code> and event handler</h3>\n<p>For those with special circumstances, they might sprinkle a little bit of JavaScript on top and use an event handler to navigate the user programmatically:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"79637347631245260000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`<a href=&quot;/elsewhere&quot; onclick=&quot;goTo('elsewhere')&quot;>Okay ¯\\_(ツ)_/¯</a>`, `79637347631245260000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"html\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-html line-numbers\"><code class=\"language-html\"><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>a</span> <span class=\"token attr-name\">href</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>/elsewhere<span class=\"token punctuation\">\"</span></span> <span class=\"token special-attr\"><span class=\"token attr-name\">onclick</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span><span class=\"token value javascript language-javascript\"><span class=\"token function\">goTo</span><span class=\"token punctuation\">(</span><span class=\"token string\">'elsewhere'</span><span class=\"token punctuation\">)</span></span><span class=\"token punctuation\">\"</span></span></span><span class=\"token punctuation\">></span></span>Okay ¯\\_(ツ)_/¯<span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>a</span><span class=\"token punctuation\">></span></span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span></span></pre></div>\n<p>This is fine as the link stays functional and you can upgrade the functionality using JavaScript.</p>\n<h3 id=\"code-classlanguage-textacode-tag-without-code-classlanguage-texthrefcode\" style=\"position:relative;\"><a href=\"#code-classlanguage-textacode-tag-without-code-classlanguage-texthrefcode\" aria-label=\"code classlanguage textacode tag without code classlanguage texthrefcode permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><code class=\"language-text\">a</code> tag without <code class=\"language-text\">href</code></h3>\n<p>But some people decide to remove the <code class=\"language-text\">href</code> attribute. This is not a good idea because you now prevented crawlers to find out where this link goes to:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"37229267911415120000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`<a onclick=&quot;goTo('elsewhere')&quot;>Please don't ╯︿╰</a>`, `37229267911415120000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"html\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-html line-numbers\"><code class=\"language-html\"><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>a</span> <span class=\"token special-attr\"><span class=\"token attr-name\">onclick</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span><span class=\"token value javascript language-javascript\"><span class=\"token function\">goTo</span><span class=\"token punctuation\">(</span><span class=\"token string\">'elsewhere'</span><span class=\"token punctuation\">)</span></span><span class=\"token punctuation\">\"</span></span></span><span class=\"token punctuation\">></span></span>Please don't ╯︿╰<span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>a</span><span class=\"token punctuation\">></span></span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span></span></pre></div>\n<h3 id=\"code-classlanguage-textacode-tag-with-code-classlanguage-texthrefcode-but-no-link\" style=\"position:relative;\"><a href=\"#code-classlanguage-textacode-tag-with-code-classlanguage-texthrefcode-but-no-link\" aria-label=\"code classlanguage textacode tag with code classlanguage texthrefcode but no link permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><code class=\"language-text\">a</code> tag with <code class=\"language-text\">href</code> but no link</h3>\n<p>In other occasions, people use the <code class=\"language-text\">href</code> attribute without a URL or with a pseudo URL:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"28093931985923314000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`<a href=&quot;javascript:goTo('elsewhere')&quot;>Seriously? (⊙_⊙)？</a>`, `28093931985923314000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"html\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-html line-numbers\"><code class=\"language-html\"><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>a</span> <span class=\"token attr-name\">href</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>javascript:goTo('elsewhere')<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span>Seriously? (⊙_⊙)？<span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>a</span><span class=\"token punctuation\">></span></span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span></span></pre></div>\n<p>This is even worse than the previous case as it just confuses the hell out of every crawler out there.</p>\n<h3 id=\"using-code-classlanguage-textbuttoncode\" style=\"position:relative;\"><a href=\"#using-code-classlanguage-textbuttoncode\" aria-label=\"using code classlanguage textbuttoncode permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>using <code class=\"language-text\">button</code></h3>\n<p>You might think it’s a good idea to use a button to navigate the user, but let me tell you, that’s not a good idea either. The rule of thumb is that if the purpose is to do something on the current page, it could be a button. But if it’s takes users to another page or to an external site, it should be a link.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"51488823975258230000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`<button onclick=&quot;goTo('elsewhere')&quot;>But why? <( _ _ )> </button>`, `51488823975258230000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"html\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-html line-numbers\"><code class=\"language-html\"><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>button</span> <span class=\"token special-attr\"><span class=\"token attr-name\">onclick</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span><span class=\"token value javascript language-javascript\"><span class=\"token function\">goTo</span><span class=\"token punctuation\">(</span><span class=\"token string\">'elsewhere'</span><span class=\"token punctuation\">)</span></span><span class=\"token punctuation\">\"</span></span></span><span class=\"token punctuation\">></span></span>But why? <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>(</span> <span class=\"token attr-name\">_</span> <span class=\"token attr-name\">_</span> <span class=\"token attr-name\">)</span><span class=\"token punctuation\">></span></span> <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>button</span><span class=\"token punctuation\">></span></span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span></span></pre></div>\n<h3 id=\"any-other-html-element\" style=\"position:relative;\"><a href=\"#any-other-html-element\" aria-label=\"any other html element permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>any other HTML element</h3>\n<p>It’s worth noting that you shouldn’t simulate a link using any other HTML element and add a click handler with JavaScript:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"76669263857467210000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`<div onclick=&quot;goTo('elsewhere')&quot;>NOOOOOOOOOOOOOOOO!!!!!!!!!!!! (#\\`O′)</div>`, `76669263857467210000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"html\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-html line-numbers\"><code class=\"language-html\"><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>div</span> <span class=\"token special-attr\"><span class=\"token attr-name\">onclick</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span><span class=\"token value javascript language-javascript\"><span class=\"token function\">goTo</span><span class=\"token punctuation\">(</span><span class=\"token string\">'elsewhere'</span><span class=\"token punctuation\">)</span></span><span class=\"token punctuation\">\"</span></span></span><span class=\"token punctuation\">></span></span>NOOOOOOOOOOOOOOOO!!!!!!!!!!!! (#`O′)<span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>div</span><span class=\"token punctuation\">></span></span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span></span></pre></div>\n<p>This not only wrong for crawlers, but also messes up the screen readers and causes a lot of trouble for people with any vision impairment. So please just use a link with a proper URL.</p>\n<h2 id=\"what-is-a-proper-url-i-hear-you-ask\" style=\"position:relative;\"><a href=\"#what-is-a-proper-url-i-hear-you-ask\" aria-label=\"what is a proper url i hear you ask permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>What is a proper URL? I hear you ask</h2>\n<p>You saw we ruled out the pseudo URLs like <code class=\"language-text\">javascript:doSomething()</code>, but let’s take a look at URLs more closely.</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"50245902276091160000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`http://example.com/old-school-url\r\n\r\nexample.com/page#section`, `50245902276091160000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-text line-numbers\"><code class=\"language-text\">http://example.com/old-school-url\r\n\r\nexample.com/page#section</code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span></span></pre></div>\n<p>A URL contains these elements:</p>\n<ul>\n<li>A protocol (optional), which defines the protocol to be used for navigation such as <code class=\"language-text\">http</code>, or <code class=\"language-text\">https</code>.</li>\n<li>It also has a host, <code class=\"language-text\">example.com</code> or <code class=\"language-text\">yashints.dev</code>. A host is a name that one or some computers respond to. Usually it points to an IP address.</li>\n<li>It has a path to specific asset on that computer such as an HTML file, an image, CSS file or a font. But that’s not always the case, sometimes the path is handled via JavaScript and is used to replace part of the content in a page. This technique is called Single Page Application or SPA.</li>\n<li>Some URLs might contain another part such the second example above. That part which comes after the <code class=\"language-text\">#</code> sign, is called a fragment identifier. By itself a fragment identifier is not a piece of content. It just points to a specific part in the content such as a header.</li>\n</ul>\n<blockquote>\n<p>💡 Because fragments are not suppose to point to different content, crawlers ignore them altogether. This means for single page applications where they use fragment like routing, crawlers don’t follow them and that’s why people use techniques such as server side rendering or SSR to allow crawlers become aware of the page’s content.</p>\n</blockquote>\n<h2 id=\"summary\" style=\"position:relative;\"><a href=\"#summary\" aria-label=\"summary permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Summary</h2>\n<p>To sum it all up:</p>\n<ul>\n<li>Use proper link markup, <code class=\"language-text\">a</code> tag with <code class=\"language-text\">href</code> attribute.</li>\n<li>Do not use other HTML elements to navigate users.</li>\n<li>Do not omit the <code class=\"language-text\">href</code> attribute on an anchor tag.</li>\n<li>And last but not least, do not use fragment identifiers to load different content in SPAs if you can.</li>\n</ul>\n<p>Hope you’ve enjoyed reading this and thanks for drooping by. Until next article, Ta.</p>","timeToRead":4,"frontmatter":{"unformattedDate":"2020-06-03T00:00:00.000Z","date":"June 03, 2020","title":"Links and JavaScript 🔗","subtitle":null,"id":null,"author":"Yaser Adel Mehraban","gravatar":null,"canonical_url":null,"tags":["showdev","webdev","javascript","links"],"thumbnail":{"childImageSharp":{"gatsbyImageData":{"layout":"constrained","placeholder":{"fallback":"data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAFwAAAwEAAAAAAAAAAAAAAAAAAAIDBf/EABYBAQEBAAAAAAAAAAAAAAAAAAECA//aAAwDAQACEAMQAAAB2kolFjANj//EABsQAAIBBQAAAAAAAAAAAAAAAAECAwAQEiEx/9oACAEBAAEFAgAtRvlYdk1GWZj/AP/EABURAQEAAAAAAAAAAAAAAAAAAAAR/9oACAEDAQE/AVf/xAAYEQACAwAAAAAAAAAAAAAAAAAAEyExYf/aAAgBAgEBPwG4Eaf/xAAaEAEAAgMBAAAAAAAAAAAAAAABABECEDES/9oACAEBAAY/AnySnu8k7UtWf//EABsQAAICAwEAAAAAAAAAAAAAAAABESEQMXFB/9oACAEBAAE/IUsNN30tohMbB9f9CUE+n//aAAwDAQACAAMAAAAQ/wAf/8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAwEBPxAf/8QAGBEBAQADAAAAAAAAAAAAAAAAAQARITH/2gAIAQIBAT8QNsusOf/EABsQAQEAAgMBAAAAAAAAAAAAAAERACEQMUGR/9oACAEBAAE/EFumQEFNY69vknBmx+40oTQG+scLE9Gf/9k="},"backgroundColor":"transparent","images":{"fallback":{"src":"/static/ded954c9096452afa1ce0e02c9b7791f/84808/link.jpg","srcSet":"/static/ded954c9096452afa1ce0e02c9b7791f/9e0f0/link.jpg 750w,\n/static/ded954c9096452afa1ce0e02c9b7791f/84808/link.jpg 960w","sizes":"(min-width: 960px) 960px, 100vw"},"sources":[{"srcSet":"/static/ded954c9096452afa1ce0e02c9b7791f/cbeef/link.webp 750w,\n/static/ded954c9096452afa1ce0e02c9b7791f/f07e3/link.webp 960w","type":"image/webp","sizes":"(min-width: 960px) 960px, 100vw"}]},"width":960,"height":540}}},"cover":{"childImageSharp":{"gatsbyImageData":{"layout":"constrained","placeholder":{"fallback":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAIAAAA7N+mxAAAACXBIWXMAAAsTAAALEwEAmpwYAAAB7ElEQVR42h2PW2+bMBiG+dnT/sJuJ/Vid/sFk6aqW6Vo3ZrFpJiW5gSEcIo5g22IHQ4miWYmPbJeyX7e77OyComZcTNjVsbckh9xF9Eeke5IuoB0IenjZkhPfUy7iHQJbRFpg+ocy9uiUfSkccjoECE50NGrWhOVbsGcrLbTxkoaM6KH7BTXQ8EvZXtL+TVh16q9yS5lEdVG2ZlVb+NBtrx6+cOzNl85T/ruz9v+CW5ny/fZcrUJK18udbpEpzFqxoxfw6pVQFTDotPzblt0Tjl4dIz4LWI3xK4xv8bnG+JTDprLgQqPirAevVrsyWhmZwXEtZ63MGygXxuI7dLWKTufDvKRnIOaixQ8MmkSl4ignj5oYbFJmQLcUveo5hAp6+FJ8npka8ScsnexkDiVsLGwqbClQ6Zg4UHmdcoVYOVvPjVcDP0G7IuFVSzsUguYFjI4weGRw/gMi17HQi/E/DDApN9hsU6Yorr05S8wHr/BA/6hWveL7YPmwEg6DCI+BTShHZlRto+a+Pjh8vVnv+bDEp0UGDT3M3D3+W7pVi8uXgYE+Fg6OuJ69B8Z5OT4bGTnX+v+05fxu96v6LDwqDLfRHLP32ahHirglMCt1ICq+1y1UtXOVFOeOfAw8GUpWXolRPglJMAlz2b2DzOkEpNPXldzAAAAAElFTkSuQmCC"},"backgroundColor":"transparent","images":{"fallback":{"src":"/static/047a7aafd636674e13164bdd7e58da45/a2195/socialpreview.png","srcSet":"/static/047a7aafd636674e13164bdd7e58da45/cadb4/socialpreview.png 750w,\n/static/047a7aafd636674e13164bdd7e58da45/a2195/socialpreview.png 800w","sizes":"(min-width: 800px) 800px, 100vw"},"sources":[{"srcSet":"/static/047a7aafd636674e13164bdd7e58da45/11ba1/socialpreview.webp 750w,\n/static/047a7aafd636674e13164bdd7e58da45/5f042/socialpreview.webp 800w","type":"image/webp","sizes":"(min-width: 800px) 800px, 100vw"}]},"width":800,"height":400}}},"img":{"publicURL":"/static/ded954c9096452afa1ce0e02c9b7791f/link.jpg"},"socialPreview":{"publicURL":"/static/047a7aafd636674e13164bdd7e58da45/socialpreview.png"}}}},"pageContext":{"title":"Links and JavaScript 🔗","postPath":"/blog/2020/06/03/links","nextPost":"/blog/2020/06/14/scroll-to-text","previousPost":"/blog/2020/05/23/build2020"}},"staticQueryHashes":[],"slicesMap":{}}