5

I have a web form that uses reCAPTCHA to filter out robots. The form has a <script> tag that loads the captcha challenge in an <iframe>. If this script fails to load, then the captcha challenge would not appear and no one would be able to send a post.

Is there any way that I can detect whether the script is loaded so that I can turn off this feature from my side?

1
  • i would suggest you to use requirejs.org , but i think you are looking for a fast solution Commented May 7, 2012 at 2:47

3 Answers 3

5

Attach the load event to the script element which points to reCAPTCHA.

var script = document.createElement("script");

script.addEventListener("load", function() {
   // Script has loaded.
});

script.src = "/path/to/recaptcha.js";

document.body.appendChild(script);
Sign up to request clarification or add additional context in comments.

1 Comment

Can you elaborate on your answer please. Thanks.
2

You could probably find a way to do that detection, but I wouldn't recommend it, as it defeats the purpose of the captcha. A computer/robot could easily make the script not load. A hosts file entry would do the trick.

A script could also be made that just executes your handling for the failed to load case, thereby circumventing your captcha.

2 Comments

@BenHuh If you have code that tells your server when a captcha is't needed (because it failed to load), then anyone could write a script that simply executes that code whether or not the captcha has loaded. Then the captcha will no longer be needed and that user could add that modification to any bot if they wanted to abuse your form.
Oh yes, because javascript is executed client side, not server side. I agree with you. Thanks :)
1

script.addEventListener wont work on IE8 and IE7. for that you need to

  if (!script.addEventListener) {
        script.attachEvent("onload", function(){
        // script has loaded in IE 7 and 8 as well.
});
    }
    else
    {
    script.addEventListener("load", function() {
       // Script has loaded.
    });
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.