javascript - 如何解决 'FB is not defined' ?

标签 javascript facebook

有时我在加载 http://connect.facebook.net/en_US/all.js 时遇到“FB 未定义”问题

我意识到问题是因为有时我的网站无法加载该文件。所以它什么也得不到,对象 FB 实际上不存在。

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

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*/}

感谢您的回答!

最佳答案

我认为你应该解决主要问题,Facebook 提供了哪个解决方案(异步加载 SDK):

You should insert it directly after the opening tag on each page you want to load it:

<script>
  window.fbAsyncInit = function() {
    FB.init({
      appId      : 'your-app-id',
      xfbml      : true,
      version    : 'v2.1'
    });
  };

  (function(d, s, id){
     var js, fjs = d.getElementsByTagName(s)[0];
     if (d.getElementById(id)) {return;}
     js = d.createElement(s); js.id = id;
     js.src = "//connect.facebook.net/en_US/sdk.js";
     fjs.parentNode.insertBefore(js, fjs);
   }(document, 'script', 'facebook-jssdk'));
</script>

来自documentation :

The Facebook SDK for JavaScript doesn't have any standalone files that need to be downloaded or installed, instead you simply need to include a short piece of regular JavaScript in your HTML that will asynchronously load the SDK into your pages. The async load means that it does not block loading other elements of your page.

更新:使用文档中的最新代码。

关于javascript - 如何解决 'FB is not defined' ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5331165/

相关文章:

ios - 为什么 FBLogin 在模拟器中不工作? - 它留在 Safari 中?

javascript - RabbitMQ 与通过 web 的 stomp 适配器

javascript - 扩展内联 block 跳转到上面的行

javascript - 将 JSON 字符串发布到 WEB API

javascript - redirect_uri 不属于应用程序

javascript - 使用 angularjs routeprovider 进行护照 facebook 身份验证

javascript - AngularJs 重新加载页面而不是仅加载 View

javascript - FabricJS - 能够输出(到 PNG)一小部分 Canvas ,而不是整个 Canvas

java - Facebook 认证安卓

facebook - 请求对话框仅适用于 Canvas-View?