Created
March 5, 2020 07:57
-
-
Save olegdater/11bbae2235815ca4960c0c62095ce52c to your computer and use it in GitHub Desktop.
Send user's email from Tilda.cc form to Typeform
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
console.log('Success URL script modifier started'); | |
var isAlreadyWatching = {}; | |
$( document ).ready(startSuccessUrlModifier); | |
function startSuccessUrlModifier() { | |
console.log('Document is ready'); | |
var daterFormButton = 'div.tn-form__submit > button'; | |
// var emailForm = '.t-form'; | |
var emailForm1 = '#form163951777'; | |
var emailForm2 = '#form166512541'; | |
var observer = new MutationObserver(function (mutations, me) { | |
if ($(emailForm1)) watchEmailChanges(emailForm1); | |
if ($(emailForm2)) watchEmailChanges(emailForm2); | |
return; | |
// me.disconnect(); // stop observing | |
}); | |
observer.observe(document, { | |
childList: true, | |
subtree: true | |
}); | |
} | |
function watchEmailChanges(emailForm) { | |
var emailSelector = emailForm + ' .t-input-group'; | |
var originalSuccessUrl = $(emailForm).attr('data-success-url'); | |
if(isAlreadyWatching[emailForm]) { | |
$(emailSelector).unbind(); | |
$(emailSelector).off(); | |
} | |
console.log('watchEmailChanges: Original successUrl: ', originalSuccessUrl); | |
previousBindWatcher = $(emailSelector).bind("propertychange keyup input cut paste change", function() { handleEmailChanged(originalSuccessUrl, emailForm ) }); | |
previousChangeWatcher = $(emailSelector).on('change', function() { handleEmailChanged(originalSuccessUrl, emailForm ) }); | |
isAlreadyWatching[emailForm] = true; | |
} | |
function handleEmailChanged(originalSuccessUrl, emailForm ) { | |
var email = $(emailForm).find('input[name="email"]').val(); | |
var newSuccessUrl = originalSuccessUrl + '?email=' + email; | |
console.log('Email changed: ', email); | |
console.log('New success url: ', newSuccessUrl); | |
$(emailForm).attr('data-success-url', newSuccessUrl); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment