1

Facebook is banned in our country. However there are still a considerable amount of users who can work around to use Facebook. Of course, many others can't. My site uses Facebook like button and like box. If the user machine cannot access Facebook, there will be some "ugly" parts on the page, where there should be Facebook plugin.

Does anyone know how to resolve this issue? I mean how to check if the user machine can access Facebook? Any trick is appreciated.

bkaid
  • 51,465
  • 22
  • 112
  • 128
Quang Vo
  • 492
  • 1
  • 3
  • 9
  • so what happens when a user cannot reach facebook ? what is the response you get from an HTTP GET request to facebok.com ? My first try would be to turn this response into a boolean, and output different code/display depending on this boolean – Cystack Aug 31 '11 at 23:40

2 Answers2

0

You could load a hidden image on Facebook's servers and then add an onerror handler to it which well let you know if Facebook cannot be accessed:

<img src="https://graph.facebook.com/4/picture" style="display:none" onerror="doSomething();" />

PS: it's absolutely ridiculous that China blocks Facebook.

bkaid
  • 51,465
  • 22
  • 112
  • 128
  • I have the same idea. I'm trying this: $.get("http://www.facebook.com/ajax/connect/external_node_connect.php", function () { alert("load facebook success"); }); But it doesn't work. Chrome console displays an error like this: XMLHttpRequest cannot load http://www.facebook.com/ajax/connect/external_node_connect.php. Origin http://localhost:2470 is not allowed by Access-Control-Allow-Origin Do you have any idea why? – Quang Vo Aug 31 '11 at 23:49
  • That works good too if you are already using jquery. If you need a way to test it, just reroute facebook.com to something else in your hosts file. – bkaid Aug 31 '11 at 23:52
  • Yeah I can access FB at the moment (I'm in the UK now). However that error appears in Chrome console. In Fiddler that request did issue, and returned 200 status. Can you see any problem with my jquery code? – Quang Vo Aug 31 '11 at 23:56
  • Either Facebook is not allowing cross-domain requests or you need to use JSONP. Check this answer: http://stackoverflow.com/questions/3595515/xmlhttprequest-error-origin-null-is-not-allowed-by-access-control-allow-origin/3744697#3744697 – bkaid Sep 01 '11 at 00:53
0

I managed to achieve this by using Facebook API. Just initiate a query, and call FB.XFBML.parse(); when the query completes.

<script type="text/javascript"> 
 window.fbAsyncInit = function () {
 FB.init({
 appId: 'app id',
 status: false,
 cookie: true,
 xfbml: false,
 oauth: false
 });
 FB.api({
 method: 'fql.query',
 query: 'SELECT uid,name FROM user WHERE uid=1234'
 },
 function (response) {
 if (response[0].uid == '1234') { // Now that the user can connect to facebook
 FB.XFBML.parse();
 }
 });
 };
 (function() {
 var e = document.createElement('script');
 e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
 e.async = true;
 document.getElementById('fb-root').appendChild(e);
 }()); 
</script> 
Quang Vo
  • 492
  • 1
  • 3
  • 9