Is it possible for two MktoForm2 methods to run on a single page? | Community
Skip to main content
Raj_Ruprai1
New Participant
July 27, 2018
Solved

Is it possible for two MktoForm2 methods to run on a single page?

  • July 27, 2018
  • 2 replies
  • 4021 views

Hi,

As an example, we have two MktoForm2 methods that are added to a page (see example below): form.onSubmit() and form.onSuccess(). We used this Documentation​. In this example, we conditionally want to do something once the form is submitted, but not necessarily when it was submitted successfully since it is not critical to the form submission, but does need to be added to the page.

// This is added via GTM when an event is triggered such as "page url = /some/path/"

<script type="text/javascript">

  MktoForms2.whenReady(function(form) {

    form.onSubmit(function() {

      // do something.

      document.write("<div>hello</div>");

    });

  });

</script>

// This is added from our CMS when the page loads.

<script type="text/javascript">

  MktoForms2.loadForm("//app-abb.marketo.com", "082-KNA-087", 3469, function(form) {

    // On successful submission, push the dataLayer event to GTM.

    form.onSuccess(function(values, url) {

      // do something.

      location.href = url;

      return false;

     });

  });

</script>

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

And you can run MktoForms.whenReady up to 10 times without problems, but I don't understand why you'd need to here.

Just pull out the onReady (the optional 4th argument of loadForm) and put everything in one whenReady.

MktoForms2.loadForm( "//instance.example.com", "aaa-bbb-ccc", 1234 );

MktoForms2.whenReady(function(form){

  form.onSubmit(function(form){

    // submit stuff

  });

  form.onSuccess(function(vals,tyUrl){

    // success stuff

  });

});

You can't use document.write, though!!! This has nothing to do with Marketo, it's a fundamental browser rule. No document.write after the page is done. (and preferably not at all).

2 replies

SanfordWhiteman
SanfordWhitemanAccepted solution
New Participant
July 27, 2018

And you can run MktoForms.whenReady up to 10 times without problems, but I don't understand why you'd need to here.

Just pull out the onReady (the optional 4th argument of loadForm) and put everything in one whenReady.

MktoForms2.loadForm( "//instance.example.com", "aaa-bbb-ccc", 1234 );

MktoForms2.whenReady(function(form){

  form.onSubmit(function(form){

    // submit stuff

  });

  form.onSuccess(function(vals,tyUrl){

    // success stuff

  });

});

You can't use document.write, though!!! This has nothing to do with Marketo, it's a fundamental browser rule. No document.write after the page is done. (and preferably not at all).

Raj_Ruprai1
New Participant
July 27, 2018

To confirm, the code from the question should work, but you are recommending to move everything into the MktoForms2.whenReady() instead?

SanfordWhiteman
New Participant
July 27, 2018

It won't work, because you're using document.write, not because you have two MktoForms2 references.

SanfordWhiteman
New Participant
July 27, 2018

Please edit your post and highlight the code using the Advanced Editor's syntax highlighter so it's readable:

https://s3.amazonaws.com/blog-images-teknkl-com/syntax_highlighter.gif