1/*! 2Mailchimp Ajax Submit 3jQuery Plugin 4Author: Siddharth Doshi 5 6Use: 7=== 8$('#form_id').ajaxchimp(options); 9 10- Form should have one <input> element with attribute 'type=email' 11- Form should have one label element with attribute 'for=email_input_id' (used to display error/success message) 12- All options are optional. 13 14Options: 15======= 16options = { 17 language: 'en', 18 callback: callbackFunction, 19 url: 'http://blahblah.us1.list-manage.com/subscribe/post?u=5afsdhfuhdsiufdba6f8802&id=4djhfdsh99f' 20} 21 22Notes: 23===== 24To get the mailchimp JSONP url (undocumented), change 'post?' to 'post-json?' and add '&c=?' to the end. 25For e.g. 'http://blahblah.us1.list-manage.com/subscribe/post-json?u=5afsdhfuhdsiufdba6f8802&id=4djhfdsh99f&c=?', 26*/ 27 28(function ($) { 29 'use strict'; 30 31 $.ajaxChimp = { 32 responses: { 33 'You are sent to a welcome message and will be being sent our latest newsletters time to time' : 0, 34 'Please enter a value' : 1, 35 'An email address must contain a single @' : 2, 36 'The domain portion of the email address is invalid (the portion after the @: )' : 3, 37 'The username portion of the email address is invalid (the portion before the @: )' : 4, 38 'This email address looks fake or invalid. Please enter a real email address' : 5 39 }, 40 translations: { 41 'en': null 42 }, 43 init: function (selector, options) { 44 $(selector).ajaxChimp(options); 45 } 46 }; 47 48 $.fn.ajaxChimp = function (options) { 49 $(this).each(function(i, elem) { 50 var form = $(elem); 51 var email = form.find('input[type=email]'); 52 var label = form.find('label[for=' + email.attr('id') + ']'); 53 54 var settings = $.extend({ 55 'url': form.attr('action'), 56 'language': 'en' 57 }, options); 58 59 var url = settings.url.replace('/post?', '/post-json?').concat('&c=?'); 60 61 form.attr('novalidate', 'true'); 62 email.attr('name', 'EMAIL'); 63 64 form.submit(function () { 65 var msg; 66 function successCallback(resp) { 67 if (resp.result === 'success') { 68 msg = 'You are sent to a welcome message and will be being sent our latest newsletters time to time.'; 69 label.removeClass('error').addClass('valid'); 70 email.removeClass('error').addClass('valid'); 71 } else { 72 email.removeClass('valid').addClass('error'); 73 label.removeClass('valid').addClass('error'); 74 var index = -1; 75 try { 76 var parts = resp.msg.split(' - ', 2); 77 if (parts[1] === undefined) { 78 msg = resp.msg; 79 } else { 80 var i = parseInt(parts[0], 10); 81 if (i.toString() === parts[0]) { 82 index = parts[0]; 83 msg = parts[1]; 84 } else { 85 index = -1; 86 msg = resp.msg; 87 } 88 } 89 } 90 catch (e) { 91 index = -1; 92 msg = resp.msg; 93 } 94 } 95 96 // Translate and display message 97 if ( 98 settings.language !== 'en' 99 && $.ajaxChimp.responses[msg] !== undefined 100 && $.ajaxChimp.translations 101 && $.ajaxChimp.translations[settings.language] 102 && $.ajaxChimp.translations[settings.language][$.ajaxChimp.responses[msg]] 103 ) { 104 msg = $.ajaxChimp.translations[settings.language][$.ajaxChimp.responses[msg]]; 105 } 106 label.html(msg); 107 108 label.show(2000); 109 if (settings.callback) { 110 settings.callback(resp); 111 } 112 } 113 114 var data = {}; 115 var dataArray = form.serializeArray(); 116 $.each(dataArray, function (index, item) { 117 data[item.name] = item.value; 118 }); 119 120 $.ajax({
121 url: url, 122 data: data, 123 success: successCallback, 124 dataType: 'jsonp', 125 error: function (resp, text) { 126 console.log('mailchimp ajax submit error: ' + text); 127 } 128 }); 129 130 // Translate and display submit message 131 var submitMsg = 'Submitting...'; 132 if( 133 settings.language !== 'en' 134 && $.ajaxChimp.translations 135 && $.ajaxChimp.translations[settings.language] 136 && $.ajaxChimp.translations[settings.language]['submit'] 137 ) { 138 submitMsg = $.ajaxChimp.translations[settings.language]['submit']; 139 } 140 label.html(submitMsg).show(2000); 141 142 return false; 143 }); 144 }); 145 return this; 146 }; 147})(jQuery);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.