如何解决“未定义 FB"? [英] How to workaround 'FB is not defined'?

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

问题描述

有时我在加载 http://时遇到FB 未定义"问题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"运算符.除非您的 FB 对象确实包含字符串 'undefined',否则您展示的示例只是将它与字符串 'undefined' 进行比较将评估为 false!

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天全站免登陆