← All posts

Guides

How to Redirect a HubSpot Form to Different URLs Based on the Answer

June 12, 2026 · 7 min read

TL;DR

To send HubSpot form submitters to different URLs based on what they selected, use the form's onFormSubmit / onFormSubmitted callback: read the field value, then set window.location.href. The catch that trips everyone up — classic HubSpot forms live in an iframe, so you can't query the fields with document.querySelector; you must use the form object HubSpot hands you. And remember this fires after submission — to route people by location before the form, you need geo-redirects instead.

This is one of the most common HubSpot CMS questions: "I have a form with a country dropdown — how do I send UK submitters to one page and everyone else to another?" It sounds like it should be a setting. It isn't, quite, but it's a few lines of JavaScript once you know the one gotcha that wastes everyone's first afternoon.

Form submittedcountry = UKonFormSubmitread the fieldUK → /uk-pricingEU → /eu-pricing
The form submission branches on the answer and redirects accordingly.

What HubSpot gives you for free

Every HubSpot form already supports a post-submit action: show an inline thank-you message, or redirect to a page or external URL. If you want allsubmitters to land on the same place, you don't need any code — set it in the form's Options tab and you're done. Custom logic is only required when the destination depends on the visitor's answer.

The onFormSubmit method

HubSpot's JavaScript embed accepts callbacks. The one you want is onFormSubmit (fires as the form submits, giving you the form object) or onFormSubmitted(fires after, giving you the submission values). Here's the pattern most teams land on:

hbspt.forms.create({
  portalId: "XXXXXX", formId: "…", target: "#formTarget",
  onFormSubmit: function (form) {
    var country = form
      .find("[name='country_region']")
      .val();
    var dest = country === "United Kingdom"
      ? "/uk-pricing" : "/eu-pricing";
    window.location.href = dest;
  }
});

The gotcha: the form is in an iframe

Here's what costs people hours. Classic HubSpot forms are rendered inside an iframe. That means the form's input fields are notpart of your page's document — so the instinctive document.querySelector("[name=country]") returns null, every time. You have to read the value through the form reference HubSpot passes into the callback (the formargument above), which has access to the iframe's internals. If you're using onFormSubmitted, read from its submissionValues instead. Either way: never query the document directly for form fields.

A note on window.top vs window.location

Some examples use window.top.location.href to break out of the iframe. On your own HubSpot pages that works, but if the form is embedded on a third-party site, window.top can be blocked by cross-origin rules and the redirect silently fails. Prefer window.location.href unless you specifically need to escape a frame, and always test in the real placement.

onFormSubmit vs onFormSubmitted — which callback?

HubSpot fires two different callbacks around a submission, and picking the wrong one is the second most common mistake after the iframe issue. onFormSubmit runs asthe form is submitting and receives the form element, so it's the right place to read a field and decide a destination before the visitor leaves. onFormSubmitted runs after the submission completes and receives a submissionValuesobject — useful if you'd rather read the clean submitted data than reach into the form, or if you want to wait until HubSpot has confirmed the submission before redirecting. For a simple "branch on a dropdown" case either works; if you need the submission to be definitely recorded before you navigate away, prefer onFormSubmitted.

One forward-looking note: HubSpot has been rolling out newer form types that don't use an iframe. If you're on those, the field-access gotcha may not apply — but the callback pattern is the same, and building against the callbacks (rather than scraping the DOM) keeps your code working across both form generations.

Where this approach stops working

Form-based redirects are perfect for routing on something the visitor tellsyou — a country dropdown, a "what describes you" picker, a plan choice. But they have hard limits worth knowing before you build:

  • They only fire on submit.Someone browsing your pricing page who never fills in a form is never routed. If the goal is "show visitors the right regional page," the form is the wrong tool — most visitors never submit one.
  • They depend on self-selection. The visitor has to pick the right country. People in a hurry pick wrong, or skip optional fields entirely.
  • They're per-form. Every form that needs the behavior needs the script, and the field names have to stay in sync.

The complement: geo-redirects before the form

The other half of the problem — routing people by where they actually are, before they interact with anything — is solved by IP-based geo-redirects that run on page load. The two techniques pair naturally: a geo-redirect puts a German visitor on your German site to begin with, and form logic handles the finer branching once they engage (say, routing enterprise enquiries to a different booking page). One reads location automatically; the other reads intent that only the visitor can supply.

If your real goal was "send visitors to the right regional page," reach for geo-redirects rather than bending forms into a routing tool they were never meant to be. If you genuinely need to branch on a form answer, the onFormSubmit pattern above is the supported way to do it.

Geo-redirects on your HubSpot site in 5 minutes

One script tag, no code, no DNS changes. Free plan included.

Start free

Frequently asked questions

Why doesn't document.querySelector find my HubSpot form fields?

Classic HubSpot forms render inside an iframe, so the fields aren't in your page's DOM. Reach them through the form object HubSpot passes to the onFormSubmit callback (or read the submitted values from the submissionValues argument) rather than querying the document directly.

Should I use redirect-on-submit or a thank-you page?

HubSpot's built-in form options already let you send every submitter to one thank-you page or URL. You only need custom JavaScript when the destination depends on what the visitor answered — different URLs per country, plan, or use case.

Can I route visitors before they fill in the form, by location?

Not with form logic — that fires only on submit. To route by where someone is before they ever see the form, you need IP-based geo-redirects, which work on page load. The two solve different halves of the problem and are often used together.

Will window.top.location.href work if the form is embedded on another site?

Using window.top can fail or be blocked when your form is embedded cross-origin (e.g. in an iframe on a partner site). On your own HubSpot pages it's fine; for embeds, prefer window.location and test in the real placement.