Links must have a clear, valid destination
Description
This rule checks that anchor (<a>) elements have valid, meaningful href attributes that lead to a clear destination. Specifically, it identifies links with the following problematic patterns: href="#" (placeholder hash links), href="" (empty links), and href="javascript:" or any javascript: pseudo-protocol URLs. These patterns create links that either do nothing, break expected browser behavior, or rely on JavaScript event handlers without providing a fallback for users who may have JavaScript disabled or use assistive technologies.
Disabilities impacted
- Visual impairments: screen reader users expect links to have a valid destination and may be confused or frustrated when activating a link that does nothing, reloads the page unexpectedly, or triggers JavaScript without visual feedback. Screen readers announce links and users expect predictable navigation behavior.
- Cognitive disabilities: users with cognitive disabilities rely on consistent, predictable link behavior. A link with
href="#"that relies on JavaScript event handlers may appear to work for some users but fail for others, creating confusion about how to interact with the page. - Motor impairments: users who rely on keyboard navigation or voice commands expect the Enter key to activate links and navigate to a new destination.
javascript:links or emptyhrefattributes can break this expectation, causing the link to do nothing or trigger unexpected behaviors. - Users with JavaScript disabled: while not a disability, many assistive technology users disable JavaScript for performance or security reasons. Links that depend entirely on JavaScript without valid
hreffallbacks become completely non-functional for these users.
Why it matters
Links are fundamental to web navigation. The <code>href attribute tells browsers and assistive technologies where a link goes and how to handle it. Using placeholder values like # or empty strings breaks this semantic meaning. While developers often use these patterns as placeholders or to create interactive elements that look like links but act like buttons, this practice creates accessibility barriers. Users expect links to navigate to new content. When a link has no valid destination, it violates the principle of predictable behavior and can leave users confused or trapped. The correct approach is to use a <button> element for interactive actions that don’t navigate, or to provide a valid href for actual navigation links.
Coding problems and solutions
Common coding problems
- Empty href attribute:
<a href="">Link</a>– this may cause the page to reload or do nothing, depending on the browser. - Hash-only href:
<a href="#">Link</a>– this scrolls the page to the top (or does nothing) and adds#to the URL, often used as a placeholder. - JavaScript pseudo-protocol:
<a href="javascript:void(0)">or<a href="javascript:doSomething()">– these rely entirely on JavaScript and break for users without JavaScript. - Missing href attribute:
<a>Link</a>– anchors withouthrefare not interactive links and may not receive focus or be announced as links by screen readers. - Links used as buttons: using an
<a>element with JavaScript to trigger actions (like opening a modal) instead of using a<button>element.
How to fix it
Replace placeholder links with actual URLs
If the element is meant to be a navigation link, provide a valid, meaningful href attribute pointing to real content.
<!-- Valid navigation link -->
<a href="/products">View our products</a>
<!-- Link to external resource -->
<a href="https://example.com/document.pdf">Download documentation (PDF)</a>
<!-- Anchor link within the same page -->
<a href="#contact-section">Jump to contact form</a>
<!-- Link with proper destination -->
<a href="/about/team">Meet our team</a><!-- Empty href -->
<a href="">View products</a>
<!-- Hash placeholder -->
<a href="#">View products</a>
<!-- JavaScript pseudo-protocol -->
<a href="javascript:void(0)">View products</a>Use buttons for interactive actions, not links
If the element triggers an action (like opening a modal, submitting a form, toggling content) rather than navigating, use a <button> element instead of an <a> tag.
<!-- Button for actions, not navigation -->
<button type="button" onclick="openModal()">
Open registration form
</button>
<button type="button" class="accordion-trigger">
Show details
</button>
<button type="submit" form="signup-form">
Submit form
</button><!-- Link pretending to be a button -->
<a href="javascript:void(0)" onclick="openModal()">
Open registration form
</a>
<a href="#" onclick="toggleDetails(); return false;">
Show details
</a>Style buttons to look like links when needed
If you need a button that looks like a link visually, use CSS to style a <button> element appropriately.
<style>
.link-button {
background: none;
border: none;
color: blue;
text-decoration: underline;
cursor: pointer;
font: inherit;
padding: 0;
}
.link-button:hover {
text-decoration: none;
}
</style>
<button type="button" class="link-button" onclick="showHelp()">
Need help?
</button>For links with role=”button”
The rule exempts links that have role="button". However, this is still not ideal. Consider using a <button> instead for better semantics.
<!-- Link with button role (exempt from rule, but still not best practice) -->
<a href="#" role="button" onclick="toggleMenu()">Menu</a>
<!-- Better: use a button -->
<button onclick="toggleMenu()">Menu</button>Note about inline event handlers
While you may be tempted to use inline event handlers like onclick="return false;" or onclick="doSomething();" alongside problematic href values to make links workable
, this approach is not recommended.
Inline event handlers mix presentation with behavior, make code harder to maintain, and can still fail for users who navigate via keyboard or assistive technologies. Instead, use the following best practices:
- For navigation links: provide a valid
hrefwith a real URL. - For actions: use a
<button>element instead of a link. - For progressive enhancement: bind event listeners in JavaScript using
addEventListener(), not inline attributes. - Bonus tip: When binding event listeners that should only execute once (e.g., a one-time modal trigger, a first-time tour guide, or a form submission handler that should prevent duplicate submissions), use the
{ once: true }option inaddEventListener(). This automatically removes the listener after its first invocation, preventing memory leaks and unintended duplicate executions without requiring manual cleanup withremoveEventListener().
For progressive enhancement with JavaScript
If you must use JavaScript to enhance a link, provide a valid fallback href that works without JavaScript, then prevent default behavior with JavaScript.
<!-- Link works without JavaScript, enhanced with JS -->
<a href="/products/filter" id="ajax-filter-link">
Filter products
</a>
<script>
document.getElementById('ajax-filter-link').addEventListener('click', (e) => {
e.preventDefault();
// Load products via AJAX instead of full page navigation
loadProductsViaAjax();
});
</script>Known Limitations
- Dynamic href changes: this rule checks the static DOM at analysis time. Links whose
hrefattributes are modified dynamically via JavaScript may not be caught if the change occurs after the analysis. - role=”button” exemption: links with
role="button"are exempt from this rule because they intentionally behave as buttons. However, using a button element is still the preferred solution. - Single-page applications (SPAs): in Single Page Applications (SPA), links often use JavaScript to handle routing. While these links may have valid
hrefvalues (like/page), the rule does not validate whether the routing works correctly. - Hash links for same-page navigation: links with
href="#section-id"that point to existing elements with matchingidare valid. The rule only flagshref="#"(hash with no target) as problematic. - Best practice rule: This is a best practices rule (not strictly required by WCAG), but following it significantly improves reliability and predictability for all users, especially those using assistive technologies.
Resources
- W3C HTML Specification: The <a> element
- MDN Web Docs: <a> anchor element
- MDN Web Docs: <button> element
- WCAG 2.2 Understanding Success Criterion 3.2.4: Consistent Identification
- MDN Web Docs: button role
Rule
- Audit: Accessibility
- Standard: SiteLint Best Practices
- Level: Best Practice
- Success Criteria: N/A (Best Practice)
- ID:
unclear-anchor-url