On this page
If SPA links Ctrl click not working is one of your release bugs, inspect the code that intercepts navigation. A handler that calls preventDefault() on every link click can override the browser behavior users expect from Ctrl-click, Command-click, or keyboard activation.
This guide shows what to test and how to leave browser-native navigation in control when an interaction is not an ordinary in-app click. The example is illustrative and was not executed here. Official HTML anchor and DOM event documentation could not be consulted in this run, so check it against your supported browsers and framework before release.
Key Takeaways
- Use real
<a href="…">links, even when an SPA router handles ordinary clicks. - Intercept only eligible, unmodified primary-button clicks that your router can handle.
- Leave modified clicks, middle-clicks, downloads, external destinations, and links targeting another browsing context to the browser.
- Test keyboard activation and new-tab behavior in a real browser; a normal click alone is not enough.
How a click handler breaks native navigation
Consider this deliberately flawed handler:
document.querySelectorAll('a').forEach((link) => {
link.addEventListener('click', (event) => {
event.preventDefault();
navigate(link.href); // Assumes an existing SPA navigation function.
});
});
It cancels the browser's default action before asking what kind of click occurred or whether the router can handle the destination. For example, a Ctrl-click on Windows or Linux, or Command-click on macOS, can be routed in the current tab instead of opening a new one. It can also hijack external links or links intended for another tab.
Middle-button activation is a separate case: browsers commonly dispatch auxclick for it rather than click. A bad click handler does not necessarily break middle-click by itself, but interception on auxclick, mousedown, or pointerdown can interfere with it. Test the actual implementation rather than assuming all mouse buttons follow the same event path.
Advertisement
Preserve browser behavior in the handler
The following is an integration sketch, not a complete router. It expects a router object supplied by your application:
canHandle(url)synchronously returns whether the router supports the URL, including its path and query.navigate(url)synchronously starts in-app navigation and throws if it cannot do so. A real router must also manage history, rendering, failures, and accessibility.
Only anchors explicitly marked data-spa-link and target="_self" are eligible. Requiring the explicit target avoids accidentally intercepting a link affected by a document-level <base target>. Ordinary links without those attributes remain browser-managed.
<nav>
<a href="/products" target="_self" data-spa-link>Products</a>
<a href="/help" target="_self" data-spa-link>Help</a>
<a href="https://example.com/">External site</a>
</nav>
function installSpaLinkHandler(root, router) {
root.addEventListener('click', (event) => {
if (
event.defaultPrevented ||
!event.cancelable ||
event.button !== 0 ||
event.metaKey ||
event.ctrlKey ||
event.shiftKey ||
event.altKey
) {
return;
}
if (!(event.target instanceof Element)) return;
const link = event.target.closest('a[data-spa-link][href]');
if (!link || !root.contains(link)) return;
const target = link.getAttribute('target');
if (!target || target.toLowerCase() !== '_self') return;
if (link.hasAttribute('download')) return;
let url;
try {
url = new URL(link.href);
} catch {
return;
}
if (!['http:', 'https:'].includes(url.protocol)) return;
if (url.origin !== window.location.origin) return;
if (url.username || url.password || url.hash) return;
// Leave same-page navigation, including an empty fragment, to the browser.
if (
url.pathname === window.location.pathname &&
url.search === window.location.search
) {
return;
}
if (!router.canHandle(url)) return;
event.preventDefault();
try {
router.navigate(url);
} catch {
// If synchronous SPA navigation fails, try a full navigation instead.
window.location.assign(url.href);
}
});
}
Install the handler on a container that contains the links, such as a navigation element, and pass your application's router. If your framework already supplies a link component, prefer its documented navigation behavior to adding a competing global handler.
This conservative example leaves fragment navigation to the browser and does not intercept downloads or links with an unspecified target. Its catch handles only synchronous errors: if navigation can fail asynchronously, the router needs its own recovery strategy. The server must also serve the appropriate page for direct visits to SPA routes; a working in-app transition does not prove that opening or refreshing the destination will work.
Advertisement
Release test: ordinary, modified, and new-tab navigation
Run these checks in the browsers and operating systems your application supports. Use a link to an SPA route and repeat relevant checks for external links, downloads, and links intended to open in another tab.
| Interaction | What to verify |
|---|---|
| Ordinary primary-button click | The intended route opens in the current tab. If intercepted, the page renders correctly and Back and Forward work. |
| Keyboard: Tab, then Enter | The anchor receives visible focus and activation reaches the intended destination. Verify focus and page context after an in-app transition. |
| Ctrl-click on Windows or Linux; Command-click on macOS | The destination opens in a new tab according to browser settings, rather than being forced into the current tab. |
| Middle-click | The destination opens in a new tab according to browser settings; the current tab is not redirected by the SPA. |
| Context menu: open link in a new tab | The new tab loads the intended destination directly, without depending on state from the original tab. |
| Copy link address, then paste it into a new tab | The URL is meaningful and the server can serve the destination. |
The browser may open a new tab in the foreground or background depending on settings and platform. Check the destination and current-tab behavior rather than requiring one focus behavior everywhere. Also refresh a directly opened route and test Back and Forward after several SPA transitions.
For debugging, temporarily log event.type, event.button, modifier-key properties, and event.defaultPrevented in the relevant handlers. Inspect click, auxclick, and any pointer or mouse handlers that cancel events. Remove diagnostic logging before release if URLs or application state could contain sensitive information.
Common Mistakes
- Using a clickable
<div>instead of a link. It does not automatically provide the same navigation semantics, keyboard behavior, or link context menu as an anchor. - Using
href="#"as a router placeholder. Copying or opening the link in a new tab no longer reaches the intended route. - Checking only Ctrl. Command-click matters on macOS; other modifiers should generally be left to the browser too.
- Canceling events before checking eligibility. Once the default action is canceled, simply returning does not restore it.
- Assuming same-origin means router-supported. A same-origin URL may be a download, server-rendered page, or route the SPA cannot handle.
- Testing only synthetic events. Automated event dispatch can help exercise handler logic, but it does not replace real-browser checks of tab opening and browser defaults.
Best Practices
Keep interception narrow and make the real URL the source of truth. Let your router handle only destinations it recognizes, and preserve browser behavior for everything else. Account for direct loads, refreshes, history navigation, focus management, and asynchronous route failures in the router and deployment—not just in the click handler.
Conclusion
A client-side router should enhance ordinary navigation without taking away the browser's link features. Before release, test the real anchors with a mouse, keyboard, modifiers, and a direct new-tab load.
What would your domain sell for?
A free TidyURLs account unlocks the Domain Estimator. It prices a name from 191,011 real domain sales.