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 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. Commented Jun 14, 2012 at 9:09
  • @RobG, maybe it depends on how a website is blocked, because I see this problem often enough. 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()">
Sign up to request clarification or add additional context in comments.

8 Comments

But when this "onerror" will fire? Browser is trying to load it for several minutes.
@DanielJF Yes, true. There is no other way for checking whether 3rd party server is working, without making a request to that server.
Ok, thanks. I think I'll try to do the trick with $.getScript() and timers.
@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..
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?
|
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.