How can I implement an invisible reCAPTCHA

This page explains how to enable and customize the invisible reCAPTCHA on your webpage.

To invoke the invisible reCAPTCHA, you can either:

See Configurations to learn how to customize the invisible reCAPTCHA. For example, you may want to specify the language or badge location.

See Verifying the user's response to check if the user successfully solved the CAPTCHA.

Automatically bind the challenge to a button

The easiest method for using the invisible reCAPTCHA widget on your page is to include the necessary JavaScript resource and add a few attributes to your html button. The necessary attributes are a class name '', your site key in the attribute, and the name of a JavaScript callback to handle completion of the captcha in the attribute.

The script must be loaded using the HTTPS protocol and can be included from any point on the page without restriction.

Programmatically bind the challenge to a button or invoke the challenge.

Deferring the binding can be achieved by specifying your onload callback function and adding parameters to the JavaScript resource. This works the same as the normal reCAPTCHA challenge.

Programmatically invoke the challenge.

Invoking the reCAPTCHA verification programmatically can be achieved by rendering the challenge in a div with an attribute data-size='invisible' and programmatically calling execute.

  1. Create a div with data-size='invisible'.

  2. Call grecaptcha.execute from a javascript method.

    When your callback is executed, you can call the method from the JavaScript API.

    Your onload callback function must be defined before the reCAPTCHA API loads. To ensure there are no race conditions:
    • order your scripts with the callback first, and then reCAPTCHA
    • use the `async` and `defer` parameters in the `script` tags

Configuration

JavaScript resource (api.js) parameters

ParameterValueDescription
onloadOptional. The name of your callback function to be executed once all the dependencies have loaded.
renderexplicit
onload
Optional. Whether to render the widget explicitly. Defaults to onload, which will render the widget in the first tag it finds.
hlSee language codesOptional. Forces the widget to render in a specific language. Auto-detects the user's language if unspecified.

g-recaptcha tag attributes and grecaptcha.render parameters

g-recaptcha tag attributegrecaptcha.render parameterValueDefaultDescription
data-sitekeysitekeyYour sitekey.
data-badgebadgebottomright bottomleft inlinebottomrightOptional. Reposition the reCAPTCHA badge. 'inline' lets you position it with CSS.
data-sizesizeinvisibleOptional. Used to create an invisible widget bound to a div and programmatically executed.
data-tabindextabindex0Optional. The tabindex of the challenge. If other elements in your page use tabindex, it should be set to make user navigation easier.
data-callbackcallbackOptional. The name of your callback function, executed when the user submits a successful response. The token is passed to your callback.
data-expired-callbackexpired-callbackOptional. The name of your callback function, executed when the reCAPTCHA response expires and the user needs to re-verify.
data-error-callbackerror-callbackOptional. The name of your callback function, executed when reCAPTCHA encounters an error (usually network connectivity) and cannot continue until connectivity is restored. If you specify a function here, you are responsible for informing the user that they should retry.
isolatedfalseOptional. For plugin owners to not interfere with existing reCAPTCHA installations on a page. If true, this reCAPTCHA instance will be part of a separate ID space.

JavaScript API

MethodDescription
grecaptcha.render(

container,

parameters

inherit

)
Renders the container as a reCAPTCHA widget and returns the ID of the newly created widget.
container
  The HTML element to render the reCAPTCHA widget.  Specify either the ID of the container (string) or the DOM element itself.
parameters
  An object containing parameters as key=value pairs, for example, {"sitekey": "your_site_key", "theme": "light"}. See grecaptcha.render parameters.
inherit
  Use existing data-* attributes on the element if the coorsponding parameter is not specified. The values in parameter will take precedence over the attributes.
grecaptcha.execute(

opt_widget_id

)
Programmatically invoke the reCAPTCHA check. Used if the invisible reCAPTCHA is on a div instead of a button.
opt_widget_id
  Optional widget ID, defaults to the first widget created if unspecified.
grecaptcha.reset(

opt_widget_id

)
Resets the reCAPTCHA widget.
opt_widget_id
  Optional widget ID, defaults to the first widget created if unspecified.
grecaptcha.getResponse(

opt_widget_id

)
Gets the response for the reCAPTCHA widget.
opt_widget_id
  Optional widget ID, defaults to the first widget created if unspecified.

Examples

Explicit rendering after an onload callback

Invoking the invisible reCAPTCHA challenge after client side validation.