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 the url we're using for this campaign (eg. www.oursite.com/campaign) redirect all traffic to our Facebook url (eg. www.facebook.com/example). However, many workplace networks block social media sites, so before automatically redirecting I'd like to first check if the user's network allows Facebook: if yes, redirect to Facebook; if no, continue through to our url (www.oursite.com/campaign).
Any help would be greatly appreciated,
Ryan (I'm ok with PHP, newb to javascript )
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)
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";
}
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)
}
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With