最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Is there a way to detect whether an external javascript file is fully loaded? - Stack Overflow

programmeradmin4浏览0评论

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?

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?

Share asked May 7, 2012 at 2:45 Question OverflowQuestion Overflow 11.3k20 gold badges81 silver badges113 bronze badges 1
  • i would suggest you to use requirejs , but i think you are looking for a fast solution – wizztjh Commented May 7, 2012 at 2:47
Add a ment  | 

3 Answers 3

Reset to default 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);

You could probably find a way to do that detection, but I wouldn't remend it, as it defeats the purpose of the captcha. A puter/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.

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.
    });
}
发布评论

评论列表(0)

  1. 暂无评论