11

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?

7 Answers 7

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.

9
  • Since the src url is on a different domain than the containing page, your ajax request will always fail. Commented Sep 16, 2010 at 18:23
  • 2
    He's trying to do the opposite though--catch errors if it doesn't load.
    – Annie
    Commented Sep 17, 2010 at 0:29
  • 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: jsfiddle.net/J3NjY
    – Annie
    Commented Sep 17, 2010 at 0:40
  • 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
    Commented Sep 17, 2010 at 5:29
  • 1
    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
    Commented Sep 17, 2010 at 9:19
2

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.

1
  • 3
    the link you posted is gone, by any chance do you have those tips somewhere else?
    – Rodolfo
    Commented Apr 16, 2012 at 17:40
1

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.

1

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?

-1

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
5
  • 1
    He wants to suppress the errors too, though. It's arguably the bigger problem.
    – slikts
    Commented Sep 10, 2010 at 11:30
  • @Reinis I. : alert is only an example. If one detect that typeof window.FB === "undefined" one has probably the warnings in the browser body, so one can overwrite the body with another HTML code which one want. But I have no idea how the firewall work. If it redirect to another page and our page with our script will be not displayed, than we can not solve the problem with respect any JavaScript. In the case one can try to load script with jQuery.ajax and test whether the result start with <html> then we loaded ERROR page instead of JS. One can load&start JS with jQuery.getScript.
    – Oleg
    Commented Sep 10, 2010 at 12:08
  • 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.
    – slikts
    Commented Sep 10, 2010 at 12:29
  • @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
    Commented Sep 10, 2010 at 13:00
  • 1
    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.
    – slikts
    Commented Sep 10, 2010 at 13:10
-1

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);
}
2
  • 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")) Commented Sep 13, 2010 at 10:39
  • 1
    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
    Commented Sep 15, 2010 at 21:09
-1

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>

Not the answer you're looking for? Browse other questions tagged or ask your own question.