Skip to content Skip to sidebar Skip to footer

Check If Facebook Is Blocked Then Redirect

Possible Duplicate: What’s the best way to check if a website is up or not via JavaScript We're about to run a campaign through our Facebook page. Ideally we'd like to have th

Solution 1:

Facebook SDK method

Since you need to check if the user has access to facebook you could try to initialize the the Facebook SDK and base your logic on that

According to documentation window.fbAsyncInit function is called on a successful initialization of the SDK, so you might achieve your effect with something like this:

var campaignLink = "http://www.oursite.com/campaign";

window.fbAsyncInit = function() {
    // facebook sdk initialized, change link
    campaignLink = "http://www.facebook.com/example";
}

Please note that this is all theoretical and untested, you might need to read more here

https://developers.facebook.com/docs/reference/javascript/

Favicon method

This function tries to load the favicon.ico file of the supplied URL and takes it as an indicator on whether the site is accessible (by the user) or not. It assumes that a site has a favicon, but you could easily change that to another image which you know exists.. (example the facebook logo)

functionisSiteOnline(url,callback) {
    // try to load faviconvar timer = setTimeout(function(){
        // timeout after 5 secondscallback(false);
    },5000)

    var img = document.createElement("img");
    img.onload = function() {
        clearTimeout(timer);
        callback(true);
    }

    img.onerror = function() {
        clearTimeout(timer);
        callback(false);
    }

    img.src = url+"/favicon.ico";
}

Usage would be,

isSiteOnline("http://www.facebook.com",function(found){
    if(found) {
        // site is online
    }
    else {
        // site is offline (or favicon not found, or server is too slow)
    }
})

Post a Comment for "Check If Facebook Is Blocked Then Redirect"