英文:
trigger of loading recaptcha library only when user interacts but not getting the value in hidden input field
问题
正如我们都知道,页面速度在搜索引擎优化(SEO)中起着重要作用,每当您尝试实施谷歌的Recaptcha v3时,它会显著减慢您的网站速度。这就是为什么我决定只在用户与输入表单进行交互时触发加载/执行/运行reCaptcha库。实际上,这是完全可能的...
这是我的代码和网站igsavers...
<form method="post" action="/video">
{{ csrf_field() }}
<div class="control is-loading">
<input name="instauser" id="author" class="input is-rounded is-focused is-medium" type="text" placeholder="https://instagram.com/p/41SW_pmmq4/">
</div>
<input type="hidden" id="g-recaptcha-response" name="g-recaptcha-response" value="">
<div class="control has-text-centered">
<br>
<button type="submit" class="button is-danger is-active is-medium">Download  
<img src="{{ asset('svg/downloads.svg') }}" width="25px" alt="Instagram video download" />
</button>
</div>
</form>
<script type="text/javascript">
// 触发加载 api.js(recaptcha.js)脚本
var reCaptchaFocus = function() {
var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'https://www.google.com/recaptcha/api.js?render=6LdvEswUAAAAADNVG2ng4ZIfA4mfKUJiuLmLgnUy';
head.appendChild(script);
// 移除焦点以避免js错误:
// Uncaught Error: reCAPTCHA has already been rendered in this element at Object.kh
document.getElementById('author').removeEventListener('focus', reCaptchaFocus);
};
// 为我们的基本HTML表单添加初始事件监听器
document.getElementById('author').addEventListener('focus', reCaptchaFocus, false);
</script>
这段代码运行良好,但问题在于隐藏的输入字段 g-recaptcha-response
的值将保持为空。以下是在此字段中添加值的代码:
<script>
grecaptcha.ready(function() {
grecaptcha.execute('6LdvEswUAAAAADNVG2ng4ZIfA4mfKUJiuLmLgnUy', {action: 'homepage'}).then(function(token) {
document.getElementById('g-recaptcha-response').value = token;
});
});
</script>
帮助我找出需要在代码中做什么更改,以将值加载到隐藏的输入字段中。
英文:
As we all know that page speed plays an important role in SEO and whenever you tried to implement google Recaptcha v3 it will slow your site a lot. that's why I decided to trigger the loading/execution/running of the reCaptcha library only when the user interacts with the input form(s). This is, actually, quite possible ..
here is my code and website igsavers...
<form method="post" action="/video">
{{ csrf_field() }}
<div class="control is-loading">
<input name="instauser" id="author" class="input is-rounded is-focused is-medium" type="text" placeholder="https://instagram.com/p/41SW_pmmq4/">
</div>
<input type="hidden" id="g-recaptcha-response" name="g-recaptcha-response" value="">
<div class="control has-text-centered">
<br>
<button type="submit" class="button is-danger is-active is-medium">Download &nbsp
<img src="{{ asset('svg/downloads.svg') }}" width="25px" alt="Instagram video download" />
</button>
</div>
</form>
<script type="text/javascript">
// trigger loading api.js (recaptcha.js) script
var reCaptchaFocus = function() {
var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'https://www.google.com/recaptcha/api.js?render=6LdvEswUAAAAADNVG2ng4ZIfA4mfKUJiuLmLgnUy';
head.appendChild(script);
// remove focus to avoid js error:
// Uncaught Error: reCAPTCHA has already been rendered in this element at Object.kh
document.getElementById('author').removeEventListener('focus', reCaptchaFocus);
};
// add initial event listener to our basic HTML form
document.getElementById('author').addEventListener('focus', reCaptchaFocus, false);
</script>
this code is working well but the problem is that hidden input type g-recaptcha-response
value will remain empty. here is my code of adding value in this field
<script>grecaptcha.ready(function() {
grecaptcha.execute('6LdvEswUAAAAADNVG2ng4ZIfA4mfKUJiuLmLgnUy', {action: 'homepage'}).then(function(token) {
document.getElementById('g-recaptcha-response').value=token;
});
});</script>
help me to figure out what I change in the code to load the value in hidden input type.
答案1
得分: 1
你不能在没有先加载reCAPTCHA脚本的情况下使用grecaptcha.ready
事件。你应该将以下代码段移动到加载脚本之后:
<script type="text/javascript">
var reCaptchaFocus = function() {
var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'https://www.google.com/recaptcha/api.js?render=6LdvEswUAAAAADNVG2ng4ZIfA4mfKUJiuLmLgnUy';
head.appendChild(script);
var recaptchaInterval = setInterval(function() {
if( !window.grecaptcha || !window.grecaptcha.execute ) { return; }
clearInterval( recaptchaInterval );
grecaptcha.execute('6LdvEswUAAAAADNVG2ng4ZIfA4mfKUJiuLmLgnUy', {action: 'homepage'}).then(function(token) {
document.getElementById('g-recaptcha-response').value=token;
});
}, 100 );
document.getElementById('author').removeEventListener('focus', reCaptchaFocus);
};
document.getElementById('author').addEventListener('focus', reCaptchaFocus, false);
</script>
英文:
You can't use the grecaptcha.ready
event without fist loading the recaptcha script. You should remove
<script>grecaptcha.ready(function() {
grecaptcha.execute('6LdvEswUAAAAADNVG2ng4ZIfA4mfKUJiuLmLgnUy', {action: 'homepage'}).then(function(token) {
document.getElementById('g-recaptcha-response').value=token;
});
});</script>
And place it after you loaded the script:
<script type="text/javascript">
var reCaptchaFocus = function() {
var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'https://www.google.com/recaptcha/api.js?render=6LdvEswUAAAAADNVG2ng4ZIfA4mfKUJiuLmLgnUy';
head.appendChild(script);
var recaptchaInterval = setInterval(function() {
if( !window.grecaptcha || !window.grecaptcha.execute ) { return; }
clearInterval( recaptchaInterval );
grecaptcha.execute('6LdvEswUAAAAADNVG2ng4ZIfA4mfKUJiuLmLgnUy', {action: 'homepage'}).then(function(token) {
document.getElementById('g-recaptcha-response').value=token;
});
}, 100 );
document.getElementById('author').removeEventListener('focus', reCaptchaFocus);
};
document.getElementById('author').addEventListener('focus', reCaptchaFocus, false);
</script>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论