tags:

views:

227

answers:

7

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?

+1  A: 

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.

Marc B
A: 

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
Oleg
He wants to suppress the errors too, though. It's arguably the bigger problem.
Reinis I.
Oleg
I didn't say anything about `alert()` in particular, and you can't load scripts from Facebook via Ajax because of the same origin policy.
Reinis I.
@Reinis I. but one can test with Ajax the results of loading of `http://connect.facebook.net/en_US/all.js` as a (one can use datatype 'text' if needed) and find out whether the file are blocked or not.
Oleg
No, it can't. It would be a serious browser security flaw if you could just load and parse any webpage by Ajax. There would be nothing keeping anyone from spying on users.
Reinis I.
+2  A: 

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.

Reinis I.
A: 

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);
}
Q_the_dreadlocked_ninja
This will try to load the HTML page as a script. You need to add some logic that detects whether you received Javascript or HTML (e.g. by parsing `getResponseHeader("Content-Type")`)
Long Ouyang
AFAIK, you can only get the response header from XHR (not from loading a script tag). XHR doesn't support cross-domain requests, so it can't be used to grab scripts from facebook like in the original question.
Annie
A: 

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"&gt;&lt;/script&gt;

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?

michael
+3  A: 

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.

dmitko
Since the src url is on a different domain than the containing page, your ajax request will always fail.
StriplingWarrior
http://jsfiddle.net/Gxq7B/ looks like it works, no?
dmitko
He's trying to do the opposite though--catch errors if it doesn't load.
Annie
To clarify, the problem is that he gets an HTML page if the proxy returns an html file instead of the script. If I change your jsfiddle to load an html file, the alert still fires: http://jsfiddle.net/J3NjY/
Annie
As I can see from the question Quinn wants to 'suppress the warnings' when the script is unavailable. If Quinn Wilson could join our conversation it would help :)
dmitko
I still don't see how this answer does anything to suppress warnings--you'll load the script if it's html, right?
Annie
jQuery.getScript( url, [ success(data, textStatus) ] )urlA string containing the URL to which the request is sent.success(data, textStatus)A callback function that is executed if the request succeeds.
dmitko
if it does not succeed - nothing will happen
dmitko
Actually, in the example above it does not succeed and the alert fires. So something does happen. I don't see how your example is any different than just putting '<script src..' in the html.
Annie
Are we talking about this example? http://jsfiddle.net/Gxq7B/ or that with $.ajax method?
dmitko
A: 

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"&gt;&lt;/script&gt;

<script>
// Remove error handler for facebook script
window.onerror = oldOnError;
</script>
Annie