8 Replies Latest reply on Jul 12, 2018 1:10 PM by Sanford Whiteman

    Dynamic location.href?

    Emily Burnett

      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!

        • Re: Dynamic location.href?
          Sanford Whiteman

          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 of 1 people found this helpful