Skip to content

Instantly share code, notes, and snippets.

@zahna
Last active August 8, 2017 13:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zahna/b302ee6d150405e5430855e53d5771ee to your computer and use it in GitHub Desktop.
Save zahna/b302ee6d150405e5430855e53d5771ee to your computer and use it in GitHub Desktop.
render any number of recaptchas that are on a single page
In the page somewhere:
<script type="text/javascript" defer>
var gcRender = function() {
var sitekey, gcs, i, rcks, len;
sitekey = '';
// Render all .g-recaptcha's on the page
gcs = document.getElementsByClassName('g-recaptcha');
len = gcs.length;
for (i = 0; i < len; i++) {
grecaptcha.render(gcs[i], {'sitekey' : sitekey});
}
// Add a value for the forms' recaptcha_keys
rcks = document.querySelectorAll('input[name=recaptcha_key]');
len = rcks.length;
for (i = 0; i < len; i++) {
rcks[i].value = sitekey;
}
};
</script>
<script src='https://www.google.com/recaptcha/api.js?onload=gcRender&render=explicit' async defer></script>
In each form:
<input type="hidden" name="recaptcha_key">
<div class="g-recaptcha"></div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment