diff --git a/resources/frontend/js/convertkit.js b/resources/frontend/js/convertkit.js index 7d1eb2a3f..48db9ecfe 100644 --- a/resources/frontend/js/convertkit.js +++ b/resources/frontend/js/convertkit.js @@ -162,67 +162,71 @@ window.convertKitRecaptchaFormSubmit = convertKitRecaptchaFormSubmit; /* eslint-enable no-unused-vars */ /** - * Register events + * Register events on frontend. + * + * @since 3.2.0 */ -document.addEventListener('DOMContentLoaded', function () { - // Removes `ck_subscriber_id` from the URI. - convertKitRemoveSubscriberIDFromURL(window.location.href); - - // Store subscriber ID as a cookie from the email address used when a ConvertKit Form is submitted. - document.addEventListener('click', function (e) { - // Check if the form submit button was clicked, or the span element was clicked and its parent is the form submit button. - if ( - !e.target.matches('.formkit-submit') && - (!e.target.parentElement || - !e.target.parentElement.matches('.formkit-submit')) - ) { - if (convertkit.debug) { - console.log('not a ck form'); +if (typeof convertkit !== 'undefined') { + document.addEventListener('DOMContentLoaded', function () { + // Removes `ck_subscriber_id` from the URI. + convertKitRemoveSubscriberIDFromURL(window.location.href); + + // Store subscriber ID as a cookie from the email address used when a ConvertKit Form is submitted. + document.addEventListener('click', function (e) { + // Check if the form submit button was clicked, or the span element was clicked and its parent is the form submit button. + if ( + !e.target.matches('.formkit-submit') && + (!e.target.parentElement || + !e.target.parentElement.matches('.formkit-submit')) + ) { + if (convertkit.debug) { + console.log('not a ck form'); + } + + return; } - return; - } + // Get email address. + const emailAddress = document.querySelector( + 'input[name="email_address"]' + ).value; - // Get email address. - const emailAddress = document.querySelector( - 'input[name="email_address"]' - ).value; + // If the email address is empty, don't attempt to get the subscriber ID by email. + if (!emailAddress.length) { + if (convertkit.debug) { + console.log('email empty'); + } - // If the email address is empty, don't attempt to get the subscriber ID by email. - if (!emailAddress.length) { - if (convertkit.debug) { - console.log('email empty'); + return; } - return; - } + // If the email address is invalid, don't attempt to get the subscriber ID by email. + const validator = + /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; + if (!validator.test(emailAddress.toLowerCase())) { + if (convertkit.debug) { + console.log('email not an email address'); + } - // If the email address is invalid, don't attempt to get the subscriber ID by email. - const validator = - /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; - if (!validator.test(emailAddress.toLowerCase())) { - if (convertkit.debug) { - console.log('email not an email address'); + return; } - return; - } - - // Wait a moment before sending the AJAX request. - convertKitSleep(2000); - convertStoreSubscriberEmailAsIDInCookie(emailAddress); - }); + // Wait a moment before sending the AJAX request. + convertKitSleep(2000); + convertStoreSubscriberEmailAsIDInCookie(emailAddress); + }); - // Set a cookie if any scripts with data-kit-limit-per-session attribute exist. - if ( - document.querySelectorAll('script[data-kit-limit-per-session="1"]') - .length > 0 - ) { - document.cookie = 'ck_non_inline_form_displayed=1; path=/'; - if (convertkit.debug) { - console.log( - 'Set `ck_non_inline_form_displayed` cookie for non-inline form limit' - ); + // Set a cookie if any scripts with data-kit-limit-per-session attribute exist. + if ( + document.querySelectorAll('script[data-kit-limit-per-session="1"]') + .length > 0 + ) { + document.cookie = 'ck_non_inline_form_displayed=1; path=/'; + if (convertkit.debug) { + console.log( + 'Set `ck_non_inline_form_displayed` cookie for non-inline form limit' + ); + } } - } -}); + }); +}