In Internet Explorer <9, at least, linking to a blocked resource (due to firewalls, WebSense, etc.) will return an error. For instance, if Facebook is blocked, linking to the Facebook SDK JavaScript file will result in an error in Internet Explorer.
Is it possible to test if a website is blocked in JavaScript, before linking to the external JavaScript file, for the following purposes:
- Prevent a JavaScript error from showing up in Internet Explorer
- If code depends on the external file, it can be prevented from running, to cause further JavaScript errors relating to the lack of the external library
Looking at the facebook SDK, looks like it gets injected asynchronously by inserting a script element into the head. This means all you have to do is create the callback handler and if facebook is blocked then that will never be called. You shouldn’t get any browser script error. So you should be able to do something like:
For a more general use case, a similar approach will work with any script that you can call using a JSON-P approach. This, of course, requires that the server takes a callback function as a parameter (or automatically calls a pre-named function):
UPDATE
Just came across this method, you might want to give it a shot: