MktoForms2 :: Destyle Marketo form

HTML

<script src="//app-ab09.marketo.com/js/forms2/js/forms2.min.js"></script>
<form id="mktoForm_1473"></form>

JavaScript

/*
* Create a completely barebones, user-styles-only Marketo form
* by removing inline STYLE attributes and disabling child STYLE elements
*/

MktoForms2.loadForm("//app-ab09.marketo.com", "729-DHM-431", 1473);
MktoForms2.whenReady(function (form) {
    var formEl = form.getFormElem()[0];

    // remove element styles from root and children (may want to disable this while debugging)
    for (var elsWithStyles = document.querySelectorAll('#' + formEl.id + ', #' + formEl.id + ' [style]'), i = 0, imax = elsWithStyles.length; i < imax; i++) {
        elsWithStyles[i].removeAttribute('style');
    }

    // disable all Marketo-sourced stylesheets
    for (var styleSheets = document.styleSheets, i = 0, imax = styleSheets.length; i < imax; i++) {
        var ssLoc = document.createElement('A');
        ssLoc.href = styleSheets[i].href;

        if ((ssLoc.hostname.search(/\.marketo\.com$/) !== -1) //  external STYLEs
        ||
        ((styleSheets[i].ownerNode || styleSheets[i].owningElement).parentNode == formEl)) { //  inline STYLEs within FORM tag
            styleSheets[i].disabled = false;
        }
    }
});