Skip to main content
jQuery(function ($) {
const thankYouUrl = '/thank-you/';
const storageKey = 'af_gform_redirect';
const buttonSelector = 'button.gform_button.btn-spark';
if (window.location.pathname.replace(/\/$/, '') === '/thank-you') {
return;
}
function getFormId(form) {
const input = form.querySelector('input[name="gform_submit"]');
const match = form.id.match(/^gform_(\d+)$/);
return input ? input.value : (match ? match[1] : null);
}
function redirect() {
sessionStorage.removeItem(storageKey);
window.location.href = thankYouUrl;
}
function checkConfirmation() {
const saved = sessionStorage.getItem(storageKey);
if (!saved) return;
const data = JSON.parse(saved);
if (
Date.now() - data.time > 300000 ||
data.path !== window.location.pathname
) {
sessionStorage.removeItem(storageKey);
return;
}
const id = data.id;
if (
document.getElementById('gform_confirmation_wrapper_' + id) ||
document.getElementById('gform_confirmation_message_' + id)
) {
redirect();
}
}
// Track both Gravity Forms when submitted.
document.addEventListener('submit', function (event) {
const form = event.target;
if (
!form.matches('form') ||
!form.querySelector(buttonSelector)
) {
return;
}
const id = getFormId(form);
if (!id) return;
sessionStorage.setItem(storageKey, JSON.stringify({
id: String(id),
time: Date.now(),
path: window.location.pathname
}));
}, true);
// Redirect after successful AJAX submission.
$(document).on('gform_confirmation_loaded', function (event, formId) {
const saved = sessionStorage.getItem(storageKey);
if (!saved) return;
const data = JSON.parse(saved);
if (String(formId) === data.id) {
redirect();
}
});
// Support dynamically loaded forms and confirmations.
const observer = new MutationObserver(checkConfirmation);
observer.observe(document.body, {
childList: true,
subtree: true
});
// Support non-AJAX submissions that reload the page.
checkConfirmation();
});