Created
September 6, 2016 18:46
-
-
Save braidn/f682961fb6e2d3c098f98bbe67b08eb3 to your computer and use it in GitHub Desktop.
This file contains 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
$(document).ready(function() { | |
var $footerForm = $('.m-footer-newsletter .mc-embedded-subscribe-form') | |
var $popupForm = $('.newsletter-popup-inner .mc-embedded-subscribe-form') | |
if( $footerForm.length > 0) { | |
initForm($footerForm); | |
} | |
if( $popupForm.length > 0) { | |
initForm($popupForm); | |
} | |
}); | |
function initForm($form) { | |
$form.find('input[type="submit"]').bind('click', function ( event ) { | |
if ( event ) event.preventDefault(); | |
register($form); | |
}); | |
} | |
function register($form) { | |
$.ajax({ | |
url: $form.attr('action'), | |
type: 'POST', | |
dataType: 'json', | |
data: $form.serialize(), | |
}) | |
.done(function() { | |
successHandler($form); | |
}) | |
.fail(function() { | |
failHandler($form); | |
}); | |
} | |
function successHandler($form) { | |
successMessage($form); | |
managePopup(); | |
} | |
function failHandler($form) { | |
failMessage($form); | |
managePopup(); | |
} | |
function successMessage($form) { | |
$form.find('.newsletter-success').css("display","block") | |
setTimeout('clearMessage($form, "success")', 3000) | |
} | |
function failMessage($form) { | |
$form.find('.newsletter-fail').css("display","block") | |
setTimeout('clearMessage($form, "fail")', 3000) | |
} | |
function managePopup() { | |
if ($('body').hasClass('is-newsletter-popup-open')) { | |
$('body').addClass('newsletter-popup-submitted'); | |
} | |
} | |
function clearMessage($form, messageType) { | |
$form.find('.newsletter-' + messageType).hide(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment