Check google reCaptcha service is on or off

馋奶兔 提交于 2019-12-11 12:34:38

问题


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:


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


来源:https://stackoverflow.com/questions/33473108/check-google-recaptcha-service-is-on-or-off

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!