Check google reCaptcha service is on or off

2019-08-09 04:37发布

I am using simple google recaptcha. My requirement is that if google api is not available (i.e. if google server is down, know its not usual case) means not getting any reply from google server then while loding the form I will hide the google reCaptcha wrapper and while submitting the form I don't want to validate google recaptcha.

Please suggest How can I achieve this.

1条回答
ゆ 、 Hurt°
2楼-- · 2019-08-09 05:03

Google does not provide that data (assuming they are always up).

But you could go about it this way. Dynamically load the script and check for the event existence in the callback. If no event is available then it failed. Check out the @example comment for usage.

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;
};

查看更多
登录 后发表回答