检查谷歌reCaptcha服务是否开启

时间:2015-11-02 08:05:45

标签: user-interface recaptcha

我正在使用简单的谷歌recaptcha。 我的要求是,如果谷歌api不可用(即谷歌服务器已关闭,知道它不常见的情况)意味着没有得到谷歌服务器的任何回复然后在填写表格我将隐藏谷歌reCaptcha包装,并在提交表格我不想验证google recaptcha。

请建议我如何实现这一目标。

1 个答案:

答案 0 :(得分:0)

Google不提供该数据(假设它们始终处于启动状态)。

但你可以这样做。动态加载脚本并检查回调中是否存在event。如果没有event可用,则失败。 查看@example评论的使用情况。

var setAttributes = function (el, attrs) {
/**
 * @method simple for in loop to help with creating elements programatically
 * @param {object} el - HTMLElement attributes are getting added to
 * @param {object} attrs - object literal with key/values for desired attributes
 * @example setAttributes(info,{
 *    'id' : 'info'
 *    'class' : 'my-class-name'
 * });
 */

    'use strict';
    var key;

    for (key in attrs) {
        if (attrs.hasOwnProperty(key)) {
            el.setAttribute(key, attrs[key]);
        }
    }

    return el;
};


var getScript = function (url, fullPath) {
/**
 * @method dynamically add script tags to the page.
 * @param {url} string with relative path and file name - do not include extension
 * @param {fullPath} string with absolute path
 * @example getScript('FrameAdjustChild');
 * @example getScript('','https://www.google-analytics.com/analytics.js');
 */

    'use strict';

    var setAtt, PATH = /js/, /* or wherever you keep your scripts */
        el = document.createElement('script'),
        attrs = {
            defer: true,
            src: null,
            type: 'text/javascript'
        };

    /** look for a string based, protocol agnostic, js file url */
    if (typeof fullPath === 'string' && fullPath.indexOf('http') === 0) {
        attrs.src = fullPath;
    }

    /** look for any string with at least 1 character and prefix our root js dir, then append extension */
    if (typeof url === 'string' && url.length >= 1) {
        attrs.src = PATH + url + '.js';
    }

    setAtt = setAttributes(el,attrs);

    el.addEventListener('load', function (event) {
      if (event) {
          /* status is good */
      }
      else {
        /* status is bad */
      }
    }, false);

    document.body.appendChild(el);

    return el;
};