问题
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.
回答1:
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.
回答2:
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>
来源:https://stackoverflow.com/questions/7264667/check-if-user-machine-can-access-facebook