Javascript:我的fbAsyncInit()方法永远不会被调用

Bea*_*red 9 javascript facebook fbconnect fbjs

我正在Facebook的开发者网站上直接从这个页面复制代码,并且fbAsyncInit()方法永远不会触发.我也读过这个页面,我已经通过几种不同的方式调整了代码,我无法解决这个问题.你的意见?

此外,为了它的价值,当我尝试运行此代码和Chrome(在Mac上)并运行Firebug lite时,我收到一条错误消息"无法在此页面中加载Firebug Lite"

这是代码......

<html>
<head>
</head>
<body>
<div id="fb-root"></div>
<script type="text/javascript">
  window.fbAsyncInit = function() {
    FB.init({
      appId      : '1234567890', // App ID
      channelUrl : '//localhost/test.html', // Channel File
      status     : true, // check login status
      cookie     : true, // enable cookies to allow the server to access the session
      xfbml      : true  // parse XFBML
    });

    alert("this statement never gets called either");
  };

  // Load the SDK Asynchronously
  (function(d){
     var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
     js = d.createElement('script'); js.id = id; js.async = true;
     js.src = "//connect.facebook.net/en_US/all.js";
     d.getElementsByTagName('head')[0].appendChild(js);
   }(document));
</script>
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

小智 7

我有同样的问题.//connect.facebook.net/en_US/all.js在异步加载之前,似乎我在其他地方加载了JavaScript库.

这可能会让事情有点困惑.

我删除了过早的加载标签定义,现在我很好.


Mik*_*y G 5

你有同步加载的问题吗?

    <script src="//connect.facebook.net/en_US/all.js"></script>
<script>
  FB.init({
    appId      : 'YOUR_APP_ID',
    channelUrl : '//WWW.YOUR_DOMAIN.COM/channel.html', // Channel File
    status     : true, // check login status
    cookie     : true, // enable cookies to allow the server to access the session
    xfbml      : true  // parse XFBML
  });
</script>
Run Code Online (Sandbox Code Playgroud)


小智 5

您应该更改:

 js.src = "//connect.facebook.net/en_US/all.js";
Run Code Online (Sandbox Code Playgroud)

至:

 js.src = "http://connect.facebook.net/en_US/all.js";
Run Code Online (Sandbox Code Playgroud)

  • 不好,如果您从HTTPS加载,这会破坏您的应用程序。如果必须强制使用任何协议,请确保使用HTTPS。 (28认同)