JavaScript redirect problems with same-domain URLs in HubSpot forms

I’m having trouble with JavaScript redirects in a HubSpot form integration on WordPress. The weird thing is that window.location works fine when redirecting to external sites, but it fails when trying to redirect to pages on the same domain. Subdomains work okay though.

I’m trying to create a custom redirect system that sends users to different thank you pages based on what they select in a dropdown. I’m pretty new to JavaScript so maybe I’m missing something obvious.

handleFormSubmission: function(formElement) {
  var selectedOption = $('select[name="inquiry_type_dropdown"]').val();
  
  if (selectedOption == 'Technical Help') {
    window.location = 'https://www.example.com/tech-support-thanks/';
  } else {
    window.location = 'https://www.cnn.com/';
  }
}

When I test this in the browser console, I get a syntax error on the function line. Also, I notice in the server logs that the page initially loads the correct thank you page but then immediately redirects to the homepage.

handleFormSubmission: function(formElement) {
  var selectedOption = "Technical Help";
  if (selectedOption == 'Technical Help') {
    window.location = 'https://mysite.com/tech-support-thanks';
  } else {
    window.location = 'https://www.cnn.com/';
  }
}

The server logs show this pattern:

[22/Mar/2019:10:15:20 -0500] "GET /tech-support-thanks HTTP/1.1" 200 5823

[22/Mar/2019:10:15:22 -0500] "GET / HTTP/1.1" 200 8901

Any ideas what could be causing this behavior?

hubspot’s hijacking your redirect after the form submits. try window.location.replace() instead - it blocks the back button from hitting the intermediate page and should dodge hubspot’s interference. also check your hubspot embed code for any onFormSubmitted callback that’s overriding your redirect.

This is a timing issue - your JavaScript redirect gets overridden by HubSpot’s default form handling. I’ve hit this exact problem before. HubSpot processes the form submission after your redirect runs, so it navigates away from where you want it to go. Wrap your redirect in a setTimeout with a small delay: setTimeout(function() { window.location.href = 'your-url'; }, 100); This lets HubSpot finish processing first. Also check for an onFormSubmit callback in your HubSpot form config that might conflict with your custom handler. Since external redirects work, HubSpot’s internal routing probably treats same-domain URLs differently.

The syntax error arises because handleFormSubmission: function(formElement) needs to be part of an object definition; just placing it in the console won’t work as it lacks context. As for the redirect issue, I’ve encountered similar behavior with HubSpot forms. The server logs suggest that the redirect occurs, but then there’s a follow-up to the homepage, likely driven by HubSpot’s form process interfering with your redirect. Consider using window.location.href instead of window.location, and if you can, add event.preventDefault() to prevent default form submission behavior. Additionally, review any WordPress redirect plugins or settings within HubSpot that may be affecting the redirect functionality.

I hit this exact issue when integrating HubSpot forms with custom WordPress themes. Your redirect runs but HubSpot’s own redirect logic jumps in and interrupts it. Check your HubSpot form settings and WordPress .htaccess file - you might have redirect rules catching same-domain requests. Those server logs showing the correct page load then homepage redirect? That’s a redirect loop. Try window.location.assign() instead and make sure your handleFormSubmission function is actually bound to HubSpot’s form events. Also check if any WordPress security plugins are blocking programmatic same-domain redirects - that’d explain why external URLs work fine.