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.
Regards
Jo
Solved! Go to Solution.
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.
<script>
window.MktoForms2BehaviorsRunCache = window.MktoForms2BehaviorsRunCache || {};
(function(){
var thisBehavior = "behavior1"; // choose unique function name
if (thisBehavior in window.MktoForms2BehaviorsRunCache) {
return;
} else {
window.MktoForms2BehaviorsRunCache[thisBehavior] = new Date();
}
// do your stuff here
})();
</script>
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.
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.
<script>
window.MktoForms2BehaviorsRunCache = window.MktoForms2BehaviorsRunCache || {};
(function(){
var thisBehavior = "behavior1"; // choose unique function name
if (thisBehavior in window.MktoForms2BehaviorsRunCache) {
return;
} else {
window.MktoForms2BehaviorsRunCache[thisBehavior] = new Date();
}
// do your stuff here
})();
</script>
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.
Sanford,
thanks - that is excellent.
I eventually realised that for what I was trying to achieve (pop up T&Cs with no control of anything outside the form) I could use a CSS only approach you detailed for me a while back (Pop up Lightbox from inside form ).
Cheers - and as always, thank you so much for the help.
Regards
Jo
Hi, Sanford!
Can you please help me to add to the form check for public domains? I'd like to prevent people with public email domains from submitting a form. How can I do that?
I got a javascript code example, but it breaks my form completely when I add this code with all public domains into a reach text field of the form. Please see the example code I got below. How can it be modified? Thank you!
Well, to start off, the code you're trying to use is broken in several ways. You should use the code I posted in this comment on another thread.
After you plug in the fixed code, post a link to the page w/your form if you still see unexpected behavior.
Unfortunately, we don't have front end developer in our team yet, so I'm not very much into JS coding. Thank you!