How to hide recaptcha for JavaScript enabled browsers and pass html validation

蹲街弑〆低调 提交于 2020-01-25 03:40:30

问题


I wish to use recaptcha for only non-JavaScript enabled browsers. To do so, I tried using the <noscript> tag as shown below.

<!DOCTYPE html>
<html>
    <head>
        <title>recaptcha</title>
    </head>

    <body>
        <noscript>
            <?php
                require_once('recaptcha-php-1.11/recaptchalib.php');
                echo recaptcha_get_html('xxx');
            ?>
        </noscript>
        <p>Done</p>
    </body>
</html>

When validating the code using http://validator.w3.org, I received the following errors:

Error Line 11, Column 11: The element noscript must not appear as a descendant of the noscript element.

    <noscript>

Error Line 12, Column 147: The frameborder attribute on the iframe element is obsolete. Use CSS instead.

…WAABIZRfA-fKq8eVkFjK_F" height="300" width="500" frameborder="0"></iframe><br/>

Is there anything I can do to remove these errors (at least the first one)? Also, am I better off detecting JavaScript use serverside and just not echoing the captcha?


回答1:


The reCAPTCHA PHP library will be rendering a further < noscript> tag into the DOM inside of your existing one, causing the invalid HTML - because by default it uses javascript.

Take a look here - Displaying reCAPTCHA Without Plugins - Specifically the section "Displaying reCAPTCHA Without Plugins". It shows you an example of what the HTML markup needs to look like. You could try just pasting in the code in the noscript tags...

<iframe src="http://www.google.com/recaptcha/api/noscript?k=your_public_key"
     height="300" width="500" frameborder="0"></iframe><br>
 <textarea name="recaptcha_challenge_field" rows="3" cols="40">
 </textarea>
 <input type="hidden" name="recaptcha_response_field"
     value="manual_challenge">


来源:https://stackoverflow.com/questions/18853007/how-to-hide-recaptcha-for-javascript-enabled-browsers-and-pass-html-validation

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