0

It's quite common for companies to block social networks by proxy. Now, if there is a "like" button on a page it needs a script (for instance, Facebook .js), but since the access is blocked, browser will try to load it for two minutes or so and thus the rest of the page won't load.

Is it possible to check with JavaScript whether some particular website is available and load a script only after that?

Let's say there are Facebook, Twitter and Google+ on the page, and Facebook and Twitter is blocked.

<script src="facebook-api.js">
<script src="twitter-api.js">
<script src="google-api.js">

Google+ won't load. What can we do in this situation?

3
  • 1
    why not load the scripts by ajax? then you can handle the error if it cant be loaded
    – gopi1410
    Commented Jun 14, 2012 at 8:08
  • Surely if the site is blocked then an error will be returned almost immediately and the browser moves on. I've never seen this as an issue, though I've worked on sites where all "social media" sites are blocked.
    – RobG
    Commented Jun 14, 2012 at 9:09
  • @RobG, maybe it depends on how a website is blocked, because I see this problem often enough.
    – Daniel J F
    Commented Jun 14, 2012 at 9:46

2 Answers 2

2

script triggers onerror event, when loading is not successfull. You can handle the loading errors:

<script type="text/javascript">
    function scriptLoadingErrorHandler(){
         //Do something
    }
</script>
<script src="facebook-api.js" onerror="scriptLoadingErrorHandler()">
<script src="twitter-api.js" onerror="scriptLoadingErrorHandler()">
<script src="google-api.js" onerror="scriptLoadingErrorHandler()">
8
  • But when this "onerror" will fire? Browser is trying to load it for several minutes.
    – Daniel J F
    Commented Jun 14, 2012 at 8:12
  • @DanielJF Yes, true. There is no other way for checking whether 3rd party server is working, without making a request to that server.
    – Engineer
    Commented Jun 14, 2012 at 8:16
  • Ok, thanks. I think I'll try to do the trick with $.getScript() and timers.
    – Daniel J F
    Commented Jun 14, 2012 at 8:23
  • @DanielJF You can't use timers, this is meaningless. You can't calculate the nominal time,when the server will response to your request.There are many factors, that could affect that nominal time:the ISP speed of client, current loading of 3rd party server,bandwidth,etc..
    – Engineer
    Commented Jun 14, 2012 at 8:30
  • Of course, but it's better to wait for 5 seconds and disable the buttons than to wait for 2 minutes and lose a user, don't you think?
    – Daniel J F
    Commented Jun 14, 2012 at 8:38
1

You can achive this with jQuery.getScript() function if you can use jQuery.
Take a look here.

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