leodutra
3/18/2013 - 6:58 PM

Loads current ReCaptcha version using an intelligent import and a <div>

Loads current ReCaptcha version using an intelligent import and a

/**
 * Usage:
 * - import jQuery;
 * - import this;
 * - create an element (maybe a div) with id="js-reCaptchaContainer" and data-publickey="<your public key>"
 *   ex: <div id="js-reCaptchaContainer" data-publickey="6LfSXd4SAAAAAH0wvCuC164"></div>
 */

if (this.jQuery) {
    jQuery(document).ready(function ($) {

        var el = $('#js-reCaptchaContainer');

        if (el.length && el.data('publickey')) {

            $.ajax({
                url: "http://www.google.com/recaptcha/api/js/recaptcha_ajax.js",
                dataType: "script",
                cache: true
            }).done(function (script, textStatus) {

                Recaptcha.create(el.data('publickey'), "js-reCaptchaContainer", {
                    theme: "clean",
                    lang: "pt",
                    callback: Recaptcha.focus_response_field
                });

            }).fail(function (jqxhr, settings, exception) {

              // fail message

            });
        }
    });
} else {
    alert('Recaptcha loader requires jQuery');
}