Check if Facebook is blocked and then redirect

Possible duplicate:
What is the best way to check if a site is running using JavaScript .

We are about to launch a campaign through our Facebook page. Ideally, we would like the URL we use for this campaign (e.g. www.oursite.com/campaign) to redirect all traffic to our Facebook URL (e.g. www.facebook.com/example). However, many networks in the workplace block social networking sites, so before automatically redirecting, I would like to first check if the user network allows Facebook: if so, redirect to Facebook; if not, go to our URL (www.oursite.com/campaign).

Any help would be greatly appreciated.

Ryan (I'm fine with PHP, newb for javascript)

+6
source share
1 answer

Facebook SDK Method

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

According to the documentation of window.fbAsyncInit function is called when the SDK is successfully initialized, so you can achieve your effect 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 may need to read here

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

Favicon method

This function attempts to load the favicon.ico file of the attached URL and takes it as an indicator of whether the site is accessible (by the user) or not. The site is supposed to have an icon, but you can easily change it to another image that you know exists. (E.g. facebook logo)

 function isSiteOnline(url,callback) { // try to load favicon var timer = setTimeout(function(){ // timeout after 5 seconds callback(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"; } 

Use will 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) } }) 
+7
source

All Articles