开发者

Check if user machine can access Facebook

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 appreciat开发者_StackOverflowed.


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.


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> 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜