I need to do some Munchkin API calls on a page for lead association on an external hosted landing page. I can't seem to find a callback function based on a successful asynchronous load of the Munchkin API, so I end up with some of the lead association calls failing because they take place prior to Munchkin's load.
My only work around (so far) is to use the blocking load of the Munchkin script, which works reliably, but of course is a blocking event and I can't force it to load at the bottom of the page.
Is there a callback when Munchkin loads? I'd rather not have my scripts polling for the API's availability if I can help it.
Thanks!
No polling is necessary.
You can call munchkinFunction("associateLead") once you've called Munchkin.init, which is of course possible as soon as the bootstrapper is loaded in the standard async embed.
When you call a munchkinFunction at this stage, it's added to an event stack, which is executed when the full library is loaded.
Thanks, Sanford. So I'd be looking at something like this?
<script type="text/javascript">
(function() {
var didInit = false;
function initMunchkin() {
if(didInit === false) {
didInit = true;
Munchkin.init('xxx-xxx-xxx');
// My function //
Munchkin.munchkinFunction('associateLead', {
'Email': 'email@nowhere.nu'},authHash);}
}
var s = document.createElement('script');
s.type = 'text/javascript';
s.async = true;
s.src = '//munchkin.marketo.net/munchkin.js';
s.onreadystatechange = function() {
if (this.readyState == 'complete' || this.readyState == 'loaded') {
initMunchkin();
}
};
s.onload = initMunchkin;
document.getElementsByTagName('head')[0].appendChild(s);
})();
</script>
There's a syntax error error in there but otherwise yes, this can go after init.
Munchkin.munchkinFunction('associateLead', {
'Email': 'email@nowhere.nu',
authHash
})