0

window.location will redirect to external links like google, but won't redirect to a url with the same domain from where it originates (it'll link to sub-domains, though). This came from a hubspot form, customized to redirect the user to a specific thank-you page based on their inquiry type. This is all in wordpress. There is no issue defining the choice variable. I'm new to JavaScript, thanks for any help.

onFormSubmit: function($form) {
  var choice = $('select[name="form_field_dropdown_select"]').val();
  
  if (choice == 'Support') {
    window.location = 'https://www.mycompany.com/support-thank-you/';
  } else {
    window.location = 'https://www.washingtonpost.com/';
  }
}

I'm running this code through my console because I know the choice variable is set up correctly. The error I'm getting is an Uncaught SyntaxError: Unexpected token ( on the line onFormSubmit: function($form)

onFormSubmit: function($form) {
  var choice = "Support";
  if (choice == 'Support') {
    window.location = 'https://newcloudnetworks.com/support-thank-you';
  } else {
    window.location = 'https://www.washingtonpost.com/';
  }
}

Additionally, I can see the redirect initially going to the support page but then immediately redirecting to the default/home page.

[21/Mar/2019:11:13:44 -0600] "GET /support-thank-you HTTP/1.1" 200 6015

[21/Mar/2019:11:13:46 -0600] "GET / HTTP/1.1" 200 9683

The issue has been resolved. It was an issue I had to raise hell with to Hubspot and they were able to resolve. It had nothing to do with an error in the JS code. Thanks.

  • Are you getting any error messages? What exactly is happening? –  Mar 19 '19 at 22:41
  • if understand right you'r getting a `Cross-Origin Resource Sharing` error when using `window.location`, am i right?? please clarify your question @Matthew Fall. – luiscla27 Mar 19 '19 at 22:49
  • @LuisLimas No I'm not getting any CORS errors – Matthew Fall Mar 21 '19 at 16:15
  • @ChrisG I'm not getting any specific error messages, on the form submit its ultimately defaulting to a different thank-you page I've set this form to go to in hubspot. Removing the default thank you page tied to this form in hubspot has not made a difference, though. – Matthew Fall Mar 21 '19 at 16:17

1 Answers1

0

So, you need to provide more info, but I'll assume your:

  • hostname(domain): www.mycompany.com

So you can add another check for the domain the form was used.

const myDomain = 'www.mycompany.com';
onFormSubmit: function($form) {
    var choice = $('select[name="form_field_dropdown_select"]').val();

    if (choice == 'Support') {
        let domain = window.location.hostname;
        if (myDomain == domain) {
            // change path if the form is used in your domain
            window.location.pathname = 'support-thank-you';
        } else {
            // redirect to you domain
            window.location = 'https://www.mycompany.com/support-thank-you/';
        }
    } else {
        window.location = 'https://www.washingtonpost.com/';
    }
}
T04435
  • 12,507
  • 5
  • 54
  • 54