Cool man, keep asking the good questions...
Does this script have the option to be used globally on all webpages, or do we have to individually add to each webpage with a form?
You can add it to all webpages as long as you pre-check if the forms library exists:
window.MktoForms2 && MktoForms2.whenReady(function(form){
form.addHiddenFields({"Latest_Form_Form_Webpage__c":document.location.href.split('?')[0]});
});
Hi there, is there a way to implement this script on the form itself?
Yes, but I wouldn't recommend it. See my thoughts on this other thread from just today: Is it okay to use javascript in rich text fields in forms?
Sanford I think you can help me out.
I'm looking at creating a global form for gated content and using the URL to route users to the correct content.
I may be in a bit over my head right now, but maybe you can help me out.
I placed the below into a RT field in the form... based on this thread
<script>
MktoForms2.whenReady(function(form){
form.addHiddenFields({"Latest_Form_Fill_Webpage":document.location});
})
</script>
I'm pretty sure my implementation is wrong for a few reasons.
1. when I save the RT field, marketo automatically adds to the code //[Cdata! ....
2. I think Mkto.Forms2.whenReady() only runs on submit? I'm not sure and don't know where to find documentation right now.
Thoughts?
Should I avoid using the JS in the Form Rt and just implement in the footer of all my wordpress pages + MKTO landing page templates?
I'm looking at creating a global form for gated content and using the URL to route users to the correct content.
If you're not using the field outside of the form context, you already have the {{trigger.web page}} to work with, so be sure you need to save the value before you do this at all.
form.addHiddenFields({"Latest_Form_Fill_Webpage":document.location});
Use document.location.href if you want a string. Even if document.location works because it gets toString()'d, it's not good to depend on that.
1. when I save the RT field, marketo automatically adds to the code //[Cdata! ....
That's fine, it's an old-fashioned compatibility hack that isn't necessary but it isn't affecting the code.
2. I think Mkto.Forms2.whenReady() only runs on submit? I'm not sure and don't know where to find documentation right now.
whenReady runs when the form is ready for use, not only on submit. It can be used to add onSubmit listeners but the whenReady function itself will run immediately. whenReady is the right place for what you're doing. See http://developers.marketo.com/javascript-api/forms/
Should I avoid using the JS in the Form Rt and just implement in the footer of all my wordpress pages + MKTO landing page templates?
If you only have one global form, you can keep it in the RT Area. The disadvantage (or advantage depending on your angle) is that you have to reapprove the form to make changes to the form behaviors. This means also reapproving Marketo-hosted LPs.
Many Thanks! Going to take a dive into this soon and I'll report back.
Thanks for the support!
Global form is live and logic for thank you page is implemented based on the custom field filled by the JS.
In my case looks like I ran into issues because the field had a different syntax when looking at the API convention.