@SanfordWhiteman , I've got it working like a charm - with one caveat, namely our old chum the Marketo Tracking cookie (and even that has a caveat).
I extended your original code with two things:
The Marketo approved method for deleting the tracking cookie - that is working a charm
Code to detect if the last name field is empty (this is a mandatory field in the DB, so safe to use)
My Code has ended up looking like this:
<datalist class="exported-tokens">
<option data-replace-into-mktoform-field="LastName">{{lead.Last Name}}</option>
<option data-replace-into-mktoform-field="FirstName">{{lead.First Name}}</option>
<option data-replace-into-mktoform-field="Email">{{lead.Email Address}}</option>
</datalist>
<script>
function delete_cookie (name, path, domain) {
document.cookie = name + "=" +
((path) ? ";path="+path:"")+
((domain)?";domain="+domain:"") +
";expires=Thu, 01 Jan 1970 00:00:01 GMT";
}
function delete_marketo_cookie () {
delete_cookie ( '_mkto_trk', '/', '.you.dont.need.to.know');
}
document.addEventListener("DOMContentLoaded", function() {
var arrayify = getSelection.call.bind([].slice);
function replaceWithExports(parentContainer) {
var exportedTokens = arrayify(document.querySelectorAll(".exported-tokens option")),
replaceablesByTarget = arrayify(parentContainer.querySelectorAll(".replace-with-exported-token:not(.replace-done)")),
replaceablesBySource = exportedTokens.filter(function(token){ return token.hasAttribute("data-replace-into-selector") || token.hasAttribute("data-replace-into-mktoform-field"); }),
replaceables = replaceablesByTarget.concat(replaceablesBySource);
replaceables
.forEach(function(replaceable) {
var contentSource,
contentTarget,
contentTargetType,
contentProperty;
if( replaceable.hasAttribute("data-replace-into-mktoform-field") ) {
contentSource = replaceable;
contentTarget = {};
contentTargetType = window.MktoForms2;
} else if( replaceable.hasAttribute("data-replace-into-selector") ) {
contentSource = replaceable;
contentTarget = parentContainer.querySelector(replaceable.getAttribute("data-replace-into-selector"));
contentTargetType = HTMLElement;
} else {
contentSource = exportedTokens.filter(function(token){ return token.label == replaceable.getAttribute("data-exported-token"); })[0];
contentTarget = replaceable;
contentTargetType = HTMLElement;
}
if ( !(contentSource && contentTarget && contentTargetType) ) return;
if( contentTargetType == window.MktoForms2 ) {
contentProperty = contentSource.getAttribute("data-replace-into-mktoform-field");
} else if( contentSource.hasAttribute("data-target-related-property") || contentTarget.hasAttribute("data-target-related-property") ) {
contentProperty = contentSource.getAttribute("data-target-related-property") || contentTarget.getAttribute("data-target-related-property");
} else if( contentSource.hasAttribute("data-allow-html") || contentTarget.hasAttribute("data-allow-html") ) {
contentProperty = "innerHTML";
} else {
contentProperty = "textContent";
}
contentTarget[contentProperty] = contentSource.value;
if( contentTargetType == window.MktoForms2 ) {
MktoForms2.whenReady(function(form){
form.setValuesCoerced(contentTarget);
});
} else if ( contentTargetType == HTMLElement ) {
contentTarget.classList.add("replace-done");
}
});
}
delete_marketo_cookie ();
replaceWithExports(document);
// This fires an alert if the code is invalid. Replace with a redirect to error LP when done.
if( "{{lead.Last Name}}" == "" ) {
//window.location.href = "https://you.dont.need.to.know/a_landing_page.html";
window.alert("dead soldier");
}
});
</script>
If you hunt for this comment:
// This fires an alert if the code is invalid. Replace with a redirect to error LP when done.
you'll find where I'm testing for the last name.
The first time this runs, it always comes up with the alert. Hit refresh, and the data gets loaded (the caveat on the caveat 🙂 ).
Of course, if I remove the cookie deleting stuff and run it in an incognito window, all works perfectly.
As usual, I know I'm missing something obvious, I just don't know what 🙂
Your help (as always) is immensely appreciated.
... View more