Replace script is breaking our form!

  • 13 September 2019
  • 0 replies
  • 7 views

We wrote a custom script that finds a string of text throughout our page, and replaces it with another string based on a given parameter in the url. The script works well, but after I implemented it, the form submission no longer works. After submitting, instead of seeing the confirmation page, instead I just see “{“protected_assets”:{}}” on the page and nothing is submitted. Here is my script source:

<script type="text/javascript">
(function () {
    const bodyTag = document.getElementsByTagName("body")[0];
    bodyTag.onload = function () {
        const phones = [];
        phones[0] = "800.205.7945";
        phones[250] = "800.276.5262";
        phones[251] = "800.205.7945";
        phones[252] = "800.276.5604";
        phones[253] = "800.304.7392";
        phones[255] = "800.315.4917";
        phones[257] = "800.213.5145";
        phones[258] = "800.315.3903";
        phones[259] = "800.225.0869";
        phones[260] = "800.226.9208";
        let phoneNumber = phones[0];
      
        let getUrlParameter = function (sParam) {
            const sPageURL = window.location.search.substring(1);
            const sURLVariables = sPageURL.split("&");
            let sParameterName;
            let i;

            for (i = 0; i < sURLVariables.length; i++) {
                sParameterName = sURLVariables[i].split("=");

                if (sParameterName[0] === sParam) {
                    return sParameterName[1] === undefined ? true : decodeURIComponent(sParameterName[1]);
                }
            }
        };
      
        let cidParameter = getUrlParameter("cid");
        if (typeof cidParameter != "undefined") {
            if (typeof phones[cidParameter] !== "undefined") {
                phoneNumber = phones[cidParameter];
            }
        }
      
      document.body.innerHTML = document.body.innerHTML.replace(/800.205.7945/g, phoneNumber);
    };
}());

Any help is appreciated. Thanks!


0 replies

Be the first to reply!

Reply