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 'We have sent you a confirmation email' : 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 = 'We have sent you a confirmation email'; 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 // Check if mailchimp added users' text to response. 97 if ((msg.indexOf(":") !== -1) && (msg.indexOf(")") !== -1)){ 98 var charIndex1 = msg.indexOf(":"); 99 var charIndex2 = msg.indexOf(")"); 100 msg = msg.substr(0, charIndex1+1) + " " + msg.substr(charIndex2, 1); 101 } 102 103 // Translate and display message 104 if ( 105 settings.language !== 'en' 106 && $.ajaxChimp.responses[msg] !== undefined 107 && $.ajaxChimp.translations 108 && $.ajaxChimp.translations[settings.language] 109 && $.ajaxChimp.translations[settings.language][$.ajaxChimp.responses[msg]] 110 ) { 111 msg = $.ajaxChimp.translations[settings.language][$.ajaxChimp.responses[msg]]; 112 } 113 label.html(msg); 114 115 label.show(200); 116 if (settings.callback) { 117 settings.callback(resp); 118 } 119 } 120 121 var data = {}; 122 var dataArray = form.serializeArray(); 123 $.each(dataArray, function (index, item) { 124 data[item.name] = item.value; 125 }); 126 127 $.ajax({
128 url: url, 129 data: data, 130 success: successCallback, 131 dataType: 'jsonp', 132 error: function (resp, text) { 133 console.log('mailchimp ajax submit error: ' + text); 134 } 135 }); 136 137 // Translate and display submit message 138 var submitMsg = 'Submitting...'; 139 if( 140 settings.language !== 'en' 141 && $.ajaxChimp.translations 142 && $.ajaxChimp.translations[settings.language] 143 && $.ajaxChimp.translations[settings.language]['submit'] 144 ) { 145 submitMsg = $.ajaxChimp.translations[settings.language]['submit']; 146 } 147 label.html(submitMsg).show(200); 148 149 return false; 150 }); 151 }); 152 return this; 153 }; 154})(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.