Created
February 9, 2023 12:04
-
-
Save jlapitan/98dbec01b8dc5052dd60e7daa19c2d4e to your computer and use it in GitHub Desktop.
load recaptcha when user interacts with the form
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var captchaLoaded = false; | |
$( document ).ready(function(){ | |
//Load reCAPTCHA script when a form field is focused | |
$('form input').on('focus', function() { | |
// If we have loaded script once already, exit. | |
if (captchaLoaded) | |
{ | |
return; | |
} | |
// Variable Intialization | |
console.log('reCAPTCHA script loading.'); | |
var head = document.getElementsByTagName('head')[0]; | |
var recaptchaScript = document.createElement('script'); | |
// Add the recaptcha site key here. | |
var recaptchaKey = "@settings.RecaptchaSiteKey"; | |
// Dynamically add Recaptcha Script | |
recaptchaScript.type = 'text/javascript'; | |
recaptchaScript.src = 'https://www.google.com/recaptcha/api.js?render=' + recaptchaKey; | |
// Add Recaptcha Script | |
head.appendChild(recaptchaScript); | |
//Set flag to only load once | |
captchaLoaded = true; | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment