Abhishek
Abhishek

Reputation: 1702

Facebook Reachability Check from javascript

I am integrating Login with Facebook using JS SDK, I use Following code to Load FB SDK

$.getScript('//connect.facebook.net/en_UK/all.js', function() {
            FB.init({
                appId: MY_APP_ID,
                channel: true,
                channelURL: 'https://www.fbrell.com/channel/',
                status: true,
                cookie: true, // enable cookies to allow the server to access the session
                xfbml: true // parse XFBML
            });
});

Now All I want to check whether Facebook is accessible from user's machine before Initializing SDK, Any Solution for that ?

Upvotes: 0

Views: 112

Answers (2)

alexP
alexP

Reputation: 3765

var checkURL = function (url, callback) {
    $.ajax({
        url: url,
        type: 'GET',
        timeout: 3000,
        crossDomain: true,
        dataType : "jsonp",
        jsonp: 'jsonp',
        statusCode: {
            200: function (response) {
               if(typeof(callback) === 'function') callback();
            },
            404: function (response) {
                alert('status  404 ');
            }
        }          
     });
};

Facebook

checkURL('http://facebook.com', function () { // <-- Callback function will be executed when async request was a success
    $.getScript('//connect.facebook.net/en_UK/all.js', function () {
        FB.init({
            appId: MY_APP_ID,
            channel: true,
            channelURL: 'https://www.fbrell.com/channel/',
            status: true,
            cookie: true, // enable cookies to allow the server to access the session
            xfbml: true // parse XFBML
        });
    });
});

Twitter

checkURL('http://www.twitter.com', function() {
    //Twitter Code
});

Upvotes: 1

Abhishek
Abhishek

Reputation: 1702

This is my final implementation

function checkURLAccessiblity(url, callback) {
    var isAccessAvailable;
    $.ajax({
        url: url,
        type: 'HEAD',
        success: function () {
            isAccessAvailable = true;
        },
        error: function () {
            isAccessAvailable = false;
        }
    });

    if(typeof(callback) === 'function') { 
        callback(isAccessAvailable);
    }
}

Upvotes: 0

Related Questions