2 Replies Latest reply on Jul 12, 2018 4:55 PM by Jo Pitts

    Embed JS directly in a form

    Jo Pitts

      All (and particularly the MMoS),

      can I embed JS directly on a form.

      Not a Marketo landing page or the page the form is embedded on, but directly in the form?

      I've tried throwing the script into a rich text field (and then editing the HTML), but it always throws CDATA around it (grrr).

      Your thoughts are (as always) appreciated.



        • Re: Embed JS directly in a form
          Sanford Whiteman

          You can in fact use a Rich Text area.


          The problem isn't actually the CDATA wrapper -- that's outdated, but harmless, and doesn't stop the code from running.


          The problem is that because of the way a form is built out in the DOM, your script will be injected and run twice (which can cause really bad bugs).


          So you need to take a rather primitive approach to making sure the function in a given RT area runs only once: name the function uniquely (inside the code block) and note the fact that it's already been run to a global object.


          window.MktoForms2BehaviorsRunCache = window.MktoForms2BehaviorsRunCache || {};
            var thisBehavior = "behavior1"; // choose unique function name
            if (thisBehavior in window.MktoForms2BehaviorsRunCache) {
            } else {
              window.MktoForms2BehaviorsRunCache[thisBehavior] = new Date();
            // do your stuff here


          Once you wrap the code like this it'll work reliably.


          I do greatly prefer to keep behaviors in external files, however, since they can be updated without reapproval or touching the Form Editor at all.

          2 of 2 people found this helpful