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

detect and suppress errors loading javascript file - Stack Overflow

programmeradmin2浏览0评论

I want to source a javascript file from facebook .js

The organization I work for has a firewall that blocks access to Facebook, it just goes to an html page that says "Access Denied blah blah blah"

I want to be able to put a javascript src tag <script src="http://... "> </script> and detect and suppress the warnings when the browser tries to evaluate the html as javascript.

Anyone know how?

I want to source a javascript file from facebook http://connect.facebook.net/en_US/all.js

The organization I work for has a firewall that blocks access to Facebook, it just goes to an html page that says "Access Denied blah blah blah"

I want to be able to put a javascript src tag <script src="http://... "> </script> and detect and suppress the warnings when the browser tries to evaluate the html as javascript.

Anyone know how?

Share Improve this question asked Sep 3, 2010 at 19:49 Quinn WilsonQuinn Wilson 8,2331 gold badge24 silver badges31 bronze badges
Add a comment  | 

7 Answers 7

Reset to default 4 +25

Looks like jQuery.getScript is what you need as was mentioned. Or you can manually execute:

$.ajax({
  url: url,
  dataType: 'script',
  success: function(){document.write('<script src="http://... "> </script>');}
});

And append your html on successful load with the <script></script> tag.

With the standard <script> tag, not possible. There's nothing really running at the time when the script's src is hit and content downloaded, so you can't wrap that in a try/catch block. There's some tips here on how to dynamically load scripts. Maybe the browsers will add some stuff to the DOM element created there which you can check for.

This is a workaround, not a direct answer, but you could simply set up a reverse proxy outside the firewall for Facebook and load the script from there. Instead of failing more gracefully, it would allow the script not to fail.

Try this, and see if it works for you:

<script type="text/javascript" onerror="throw('An error occurred')" src="http://connect.facebook.net/en_US/all.js"></script>

Alternatively, if you have access to a proxy script to grab external content I would use it via an xmlHttpRequest to grab the JS content. If it is successful, eval the content (yes, eval is evil, I know).

I would add that if you know the JS will fail, then why bother?

Why do you not do this in very simple way?:

if(!window.FB) { // or (typeof window.FB === "undefined")
    alert ("ERROR: http://connect.facebook.net/en_US/all.js is not loaded");
}
if(!window.jQuery) { // or (typeof window.jQuery === "undefined")
    alert ("ERROR: jQuery is not loaded");
}
// and so on

Please try the function below, it will only call the onload_function if the script has loaded. You can set a timeout to cancel the script.

function include_js(url, onload_function) { 
    var script = document.createElement("script");
    script.type = "text/javascript";

    if (script.readyState) { 
        script.onreadystatechange = function(){
            if (script.readyState == "loaded" || script.readyState == "complete"){
                script.onreadystatechange = null;
                onload_function();
            }
        };
    } else {  
        script.onload = function(){
            onload_function();
        };
    }
    script.src = url;
    document.getElementsByTagName("head")[0].appendChild(script);
}

In Firefox and IE, you should be able to use window.onerror for this. You can take advantage of the fact that scripts run in the order they are listed in the HTML to wrap an error handler around just the facebook script:

<script>
// Remember old error handler, if there is one.
var oldOnError = window.onerror;
// Special error handler for facebook script
window.onerror = function(message, url, linenumber) {
  // Potentially alert the user to problem
  alert('Problem with facebook: ...');
  // Return true to suppress default error handling
  return true;
}
</script>

<!-- Load facebook script -->
<script src="http://connect.facebook.net/en_US/all.js"></script>

<script>
// Remove error handler for facebook script
window.onerror = oldOnError;
</script>
发布评论

评论列表(0)

  1. 暂无评论