AnsweredAssumed Answered

Dynamic location.href?

Question asked by Emily Burnett on Dec 1, 2017
Latest reply on Jul 12, 2018 by Sanford Whiteman

Can I do some Javascript logic within this example code (below) to send a user to 1 of 3 external landing pages after filling out a Marketo form?

 

MktoForms2.loadForm("//app-sjst.marketo.com", "785-UHP-775", 1057, function(form) {
    //Add an onSuccess handler
    form.onSuccess(function(values, followUpUrl) {
        // Take the lead to a different page on successful submit, ignoring the form's configured followUpUrl
        location.href = "https://google.com/?q=marketo+forms+v2+examples";
        // Return false to prevent the submission handler continuing with its own processing
        return false;
    });
});

 

Further description of my scenario: I have a Marketo form embedded on an external landing page and, depending on which boxes are check, want to send them to 1 of 3 follow-up pages. Basically,

1 . If some of the first four options are checked, go to one page

2. If all of the first four options are check, go to another

3. Else, go to the third page.

 

form.onSuccess(function(values, followUpURL) {
  var $jQ = jQuery.noConflict(); 

  $jQ(document).ready(function() {
    var unsubscribed = $jQ('#Unsubscribed').attr('value');
    var subscriptionUpdates = $jQ('#subscriptionUpdates').attr('value');
    var subscriptionNewsletter = $jQ('#subscriptionNewsletter').attr('value');
    var subscriptionEvents = $jQ('#subscriptionEvents').attr('value');
    var subscriptionWebinars = $jQ('#subscriptionWebinars').attr('value');
  });
  if (subscriptionUpdates === 'yes' || subscriptionNewsletter === 'yes' || subscriptionEvents === 'yes' || subscriptionWebinars === 'yes') {
      location.href = "/email-preferences-some";
  } 
  else if (subscriptionUpdates === 'yes' && subscriptionNewsletter === 'yes' && subscriptionEvents === 'yes' && subscriptionWebinars === 'yes') {
      location.href = "/email-preferences-all";
  }
  else {
      location.href = "/email-preferences-unsubscribed";
  }
  return false;
});
});
</script>

The result of this code is that, on form submission, it goes to the else and no matter what I check, goes to /email-preferences-unsubscribed.

 

Main question: is there a better way to be accomplishing this? And if this is sound, why does the above not work? Really appreciate any help!

Outcomes