如何解决“FB没有定义”? [英] How to workaround 'FB is not defined'?

查看:183
本文介绍了如何解决“FB没有定义”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时候,在加载 http:// connect.facebook.net/en_US/all.js

我已经意识到问题是因为有时我的网站没有加载该文件。所以它什么也没有,对象FB字面上不存在。

I've realized that the problem is because sometimes my website just doesn't load that file. So it gets nothing, and the object FB literally doesn't exist.

我的解决方案是在发生这种情况时阻止我的用户,所以我尝试了以下代码JavaScript但没有似乎有效:

My solution is to prevent my users when this happens, so I've tried the following codes in JavaScript but none seems to work:

if (FB) {/*run the app*/} else {/*alert the user*/}
if (FB!==false) {/*run the app*/} else {/*alert the user*/}
if (FB!='undefined') {/*run the app*/} else {/*alert the user*/}

感谢答案

推荐答案

假设FB是一个包含Facebook对象的变量,我会尝试这样的:

Assuming FB is a variable containing the Facebook object, I'd try something like this:

if (typeof(FB) != 'undefined'
     && FB != null ) {
    // run the app
} else {
    // alert the user
}

为了测试某些东西在普通旧JavaScript中未定义,您应该使用typeof运算符。您显示的样本与undefined字符串进行比较将会计算为false,除非您的FB对象确实包含字符串'undefined'!

In order to test that something is undefined in plain old JavaScript, you should use the "typeof" operator. The sample you show where you just compare it to the string 'undefined' will evaluate to false unless your FB object really does contain the string 'undefined'!

您可能希望使用各种各样的工具,如Firebug(在Firefox中)来查看是否可以找出为什么不加载Facebook文件。

As an aside, you may wish to use various tools like Firebug (in Firefox) to see if you can work out why the Facebook file is not loading.

这篇关于如何解决“FB没有定义”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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