Why it matters
- Google's JavaScript SEO guide is explicit: Google can only discover links that are
<a>elements with anhrefattribute. A click handler, adata-hrefor ajavascript:address works for a visitor and is not a link to a crawler. - When the menu or the pagination is built this way, the pages behind it get no link from this page. They may be found late, through other links or the sitemap, or not at all.
- Screen readers and keyboards meet the same problem: an element without
hrefis not a link to them unless the page adds a role and a tabindex.
Example
<a onclick="location.href = '/ar/oud/'">العود</a>
<a href="/ar/oud/">العود</a>
How to fix
Give every link an <a> with a real address, and keep any script on top of it:
html
<a href="/ar/oud/">العود</a>- In a single-page app, render
<a href>and let the router intercept the click; Next.jsLink, Vue'sRouterLinkand Angular'srouterLinkdo this. - An Arabic address is fine as it is, written in Arabic or percent-encoded: what matters is that it is in
href. - A button that opens a menu or adds to the cart is not a link: make it a
<button>.
How we detect
- We read the HTML as the server sends it.
- We report an
<a>withouthrefthat a script makes work (onclick,data-href,data-url,data-link, or a framework's click binding), and one in<nav>,role="navigation"or a pagination block; an element withrole="link"and nohref; and ajavascript:hrefthat names a place to go (a location, a path,window.open). javascript:void(0)and other calls with no address,<a name>anchors,<a>withouthrefor script outside a menu, and links whosehrefa framework writes (routerLink,ng-href,:href) are not reported.
Links a script adds after the page loads are not read here.