检查Facebook是否被阻止,然后重定向 [英] Check if Facebook is blocked then redirect

查看:33
本文介绍了检查Facebook是否被阻止,然后重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
是什么通过JavaScript检查网站是否正常的最佳方法

我们将通过我们的Facebook页面进行广告系列.理想情况下,我们希望有用于此广告系列的网址(例如www.oursite.com/campaign)将所有流量重定向到我们的Facebook网址(例如www.facebook.com/example).但是,许多工作场所网络都阻止了社交媒体网站,因此在自动重定向之前,我想先检查用户的网络是否允许Facebook:如果可以,请重定向到Facebook;如果不是,请重定向到Facebook.如果没有,请继续浏览我们的网址(www.oursite.com/campaign).

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).

任何帮助将不胜感激,

瑞安(我对PHP表示满意,对JavaScript来说是newb)

Ryan (I'm ok with PHP, newb to javascript )

推荐答案

Facebook SDK方法

由于您需要检查用户是否具有访问Facebook的权限,因此可以尝试初始化Facebook SDK ,并以此为基础

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

根据文档 window.fbAsyncInit 函数在成功初始化SDK时被调用,因此您可以通过以下方式实现效果:

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/

网站图标方法

此功能尝试加载所提供URL的favicon.ico文件,并将其用作指示该站点是否(用户)可访问的指示器.它假定一个站点上有一个网站图标,但是您可以轻松地将其更改为另一个已知的图像.(例如,Facebook徽标)

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";
}

用法是

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)
    }
})

这篇关于检查Facebook是否被阻止,然后重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆