Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How can I stop a link from navigating while handling its click?

Quick answer

Call event.preventDefault() inside the link’s click handler when your code is taking over an action the link would normally perform, such as showing an app view instead of navigating to a new page. For ordinary links that should open their destination, do not cancel the default action. Google’s example uses this approach for single-page-app routing and updates browser history with pushState; it does not cover every event-handling edge case. [S1]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

What do I actually need to call to stop a link from following its URL when it is clicked?

Geez

Call preventDefault() on the click event, inside the handler for that link. For example:

const link = document.querySelector("#details-link");

link.addEventListener("click", (event) => {
  event.preventDefault();
  showDetails();
});

Here, showDetails() is a placeholder for the action your page actually performs. The important pairing is that the handler cancels the link’s normal navigation and then does something useful instead. Google’s single-page-app example uses event.preventDefault() when the app handles the link by loading a view and updating browser history. [S1]

Follow-up question

I only want to stop navigation for links my app handles. Should every link use that code?

Geez

No. Leave ordinary links alone when clicking them should navigate to their destination. Cancel only a link whose intended action your code is actually handling—for example, an app route that displays another view without a normal page navigation. [S1]

A useful way to decide is to ask: “If JavaScript did not take over this click, would the link’s destination still be the right result?” If yes, preserve the normal link behavior. If your app is deliberately taking over that route, the handler can cancel navigation and perform the app action. Avoid adding a blanket handler that calls preventDefault() for every link; that can suppress links you meant to keep working normally.

Follow-up question

Can you show what that looks like for a link that opens a different view in a small app?

Geez

Assume this link represents an in-app route and that your application supplies showView():

<a href="/help" id="help-link">Help</a>
const link = document.querySelector("#help-link");

link.addEventListener("click", (event) => {
  event.preventDefault();
  showView("help");
  history.pushState({}, "", "/help");
});

The example illustrates the same broad pattern as Google’s single-page-app guidance: cancel the link’s browser navigation, load the requested view, and update history with pushState. [S1] showView() is an assumed placeholder, not a built-in function. The cited example does not establish how to handle every browser event edge case, so treat this as a basic pattern rather than a complete router.

Follow-up question

What should I check if the click handler runs but the page still navigates—or if links stop working?

Geez

Check that the handler is attached to the link you clicked, that preventDefault() is called on that handler’s event, and that the handler only cancels navigation when your code provides the intended alternative. If you want a link to navigate normally, remove the cancellation for that link. If you are building a client-side route, confirm that your route-handling code performs the intended view change; the cited Google example demonstrates a routing pattern but does not explain all event cases or troubleshoot a particular page. [S1]

Before keeping the change, test one app-handled link and one ordinary link. The first should perform your app’s action; the second should retain its intended navigation.

A practical checklist

  • Decide whether the link represents an app-handled action or ordinary navigation.

  • Attach the click handler to the intended link.

  • Call event.preventDefault() only when your code takes over the link’s action.

  • Provide the replacement action, such as showing the intended app view.

  • Test an app-handled link and an ordinary link separately.

A few more useful answers

Does `preventDefault()` itself load the new view?

No. In the example, it cancels the link’s default navigation; your application must supply the view-changing action.

Should I cancel navigation on every link in a single-page app?

Do so only for links your app is handling. Preserve normal navigation for links that should follow their destination. [S1]

Does the example cover every browser event edge case?

No. The cited guidance demonstrates a single-page-app routing pattern, but it does not explain all event-handling edge cases. [S1]

Sources

References used for this guide.

  1. [S1] Understand the JavaScript SEO basics — Google Search Central
    developers.google.com · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work