Dynamic location.href? | Community
Skip to main content
December 1, 2017
Solved

Dynamic location.href?

  • December 1, 2017
  • 1 reply
  • 6026 views

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!

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by SanfordWhiteman

You must not use jQuery (or even plain DOM) selectors to get field values from Marketo forms.  The results will be unreliable.

More important, the values object (first argument) is right there for you to use in onSuccess, which always contains the submitted values.

Examining the code, you have an unreachable else {} branch because you check the ors (||) before you check the ands (&&). That is:

if ( a || b || c) {

  // case 1

} else if ( a && b && c ) {

  // case 2

}

This code can never reach case 2 because if either a or b or c is true, that matches case 1.

You haven't provided a link to your running form (I can double-check it if you do) but based on your excerpt, you want this:

form.onSuccess(function(values,followUpURL){

  var subFields = [

      'subscriptionUpdates',

      'subscriptionNewsletter',

      'subscriptionEvents',

      'subscriptionWebinars'

    ],

    subValues = subFields.map(function(field){ return values[field]; }),

    subsAllYes = subValues.every(function(val){ return val == 'yes'; }),

    subsSomeYes = subValues.some(function(val){ return val == 'yes'; });

   

    if (subsAllYes) {

      location.href = "/email-preferences-all";     

    } else if (subsSomeYes) {

      location.href = "/email-preferences-some";     

    } else {

      location.href = "/email-preferences-unsubscribed";     

    }

   

    return false;

})

1 reply

SanfordWhiteman
SanfordWhitemanAccepted solution
New Participant
December 2, 2017

You must not use jQuery (or even plain DOM) selectors to get field values from Marketo forms.  The results will be unreliable.

More important, the values object (first argument) is right there for you to use in onSuccess, which always contains the submitted values.

Examining the code, you have an unreachable else {} branch because you check the ors (||) before you check the ands (&&). That is:

if ( a || b || c) {

  // case 1

} else if ( a && b && c ) {

  // case 2

}

This code can never reach case 2 because if either a or b or c is true, that matches case 1.

You haven't provided a link to your running form (I can double-check it if you do) but based on your excerpt, you want this:

form.onSuccess(function(values,followUpURL){

  var subFields = [

      'subscriptionUpdates',

      'subscriptionNewsletter',

      'subscriptionEvents',

      'subscriptionWebinars'

    ],

    subValues = subFields.map(function(field){ return values[field]; }),

    subsAllYes = subValues.every(function(val){ return val == 'yes'; }),

    subsSomeYes = subValues.some(function(val){ return val == 'yes'; });

   

    if (subsAllYes) {

      location.href = "/email-preferences-all";     

    } else if (subsSomeYes) {

      location.href = "/email-preferences-some";     

    } else {

      location.href = "/email-preferences-unsubscribed";     

    }

   

    return false;

})

December 4, 2017

Thanks so much @Sanford Whiteman​! Appears to be working like a charm. Really appreciate the help and also the info about what not to do.

SanfordWhiteman
New Participant
December 4, 2017

Cool, if you could mark my answer as Correct when you get a chance to help future searches...