简体   繁体   English

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

[英]Javascript: My fbAsyncInit() method never gets called

I'm copying the code right out of this page on Facebook's developer website and the fbAsyncInit() method never fires. 我正在Facebook的开发者网站上直接从这个页面复制代码,并且fbAsyncInit()方法永远不会触发。 I've also read this page , I've tweaked the code quite a few different ways, and I cannot get the method to fire. 我也读过这个页面 ,我已经通过几种不同的方式调整了代码,我无法解决这个问题。 Your thoughts? 你的意见?

Also, for what it's worth, when I try and run this code and Chrome (on Mac) and run Firebug lite, I get an error that says "Firebug Lite cannot be loaded in this page" 此外,为了它的价值,当我尝试运行此代码和Chrome(在Mac上)并运行Firebug lite时,我收到一条错误消息“无法在此页面中加载Firebug Lite”

Here's the code... 这是代码......

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

I had the same problem. 我有同样的问题。 It seemed I loaded the JavaScript library //connect.facebook.net/en_US/all.js in some other place before the async loading. 在异步加载之前,似乎我在其他地方加载了JavaScript库//connect.facebook.net/en_US/all.js

That probably confused things a bit. 这可能会让事情有点困惑。

I removed the premature load tag definition and now I am fine. 我删除了过早的加载标签定义,现在我很好。

You should change: 你应该改变:

 js.src = "//connect.facebook.net/en_US/all.js";

to: 至:

 js.src = "http://connect.facebook.net/en_US/all.js";

Do you have a problem with Synchronous loading? 你有同步加载的问题吗?

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

这个答案可能为时已晚,而且对于很多情况都不会有帮助但是,我发现我的Firefox浏览器经过一些体面的工作后显然有点疯狂并导致错误...只是重启该死的东西

Please Change Your App ID Then Start Working 请更改您的应用程序ID然后开始工作

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

  (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 = "https://connect.facebook.net/en_US/sdk.js";
     fjs.parentNode.insertBefore(js, fjs);
   }(document, 'script', 'facebook-jssdk'));
</script>

Since both your file is called test.html and the channelUrl is supposed to be test.html, you're creating a circular reference . 由于您的文件都被称为test.html,而channelUrl应该是test.html,因此您正在创建循环引用 For the proper channelUrl it is supposed to just contain one line 对于正确的channelUrl,它应该只包含一行

<script src="//connect.facebook.net/en_US/all.js"></script>

http://developers.facebook.com/docs/reference/javascript/ http://developers.facebook.com/docs/reference/javascript/

The channel file addresses some issues with cross domain communication in certain browsers. 通道文件解决了某些浏览器中跨域通信的一些问题。 The contents of the channel.html file can be just a single line: channel.html文件的内容只能是一行:

<script src="//connect.facebook.net/en_US/all.js"></script>

It is important for the channel file to be cached for as long as possible. 尽可能长时间地缓存通道文件非常重要。 When serving this file, you must send valid Expires headers with a long expiration period. 提供此文件时,您必须发送有效期限过长的有效Expires标头。 This will ensure the channel file is cached by the browser which is important for a smooth user experience. 这将确保浏览器缓存频道文件,这对于流畅的用户体验非常重要。 Without proper caching, cross domain communication will become very slow and users will suffer a severely degraded experience. 如果没有适当的缓存,跨域通信将变得非常缓慢,用户将遭受严重降级的体验。 A simple way to do this in PHP is: 在PHP中执行此操作的简单方法是:

 <?php  $cache_expire = 60*60*24*365;  header("Pragma: public");  header("Cache-Control: max-age=".$cache_expire);  header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$cache_expire) . ' GMT');  ?>  <script src="//connect.facebook.net/en_US/all.js"></script>

The channelUrl parameter is optional, but recommended. channelUrl参数是可选的,但建议使用。 Providing a channel file can help address three specific known issues. 提供频道文件有助于解决三个特定的已知问题。 First, pages that include code to communicate across frames may cause Social Plugins to show up as blank without a channelUrl. 首先,包含跨帧通信的代码的页面可能会导致社交插件在没有channelUrl的情况下显示为空白。 Second, if no channelUrl is provided and a page includes auto-playing audio or video, the user may hear two streams of audio because the page has been loaded a second time in the background for cross domain communication. 其次,如果没有提供channelUrl并且页面包括自动播放音频或视频,则用户可以听到两个音频流,因为该页面已经在后台第二次加载以进行跨域通信。 Third, a channel file will prevent inclusion of extra hits in your server-side logs. 第三,通道文件将阻止在服务器端日志中包含额外的命中。 If you do not specify a channelUrl, you can remove page views containing fb_xd_bust or fb_xd_fragment parameters from your logs to ensure proper counts. 如果未指定channelUrl,则可以从日志中删除包含fb_xd_bust或fb_xd_fragment参数的页面视图,以确保正确计数。

The channelUrl must be a fully qualified URL matching the page on which you include the SDK. channelUrl必须是与您包含SDK的页面匹配的完全限定URL。 In other words, the channel file domain must include www if your site is served using www, and if you modify document.domain on your page you must make the same document.domain change in the channel.html file as well. 换句话说,如果您的网站使用www提供服务,则渠道文件域必须包含www,如果您在网页上修改document.domain,则必须在channel.html文件中进行相同的document.domain更改。 The protocols must also match. 协议也必须匹配。 If your page is served over https, your channelUrl must also be https. 如果您的网页是通过https投放的,那么您的channelUrl也必须是https。 Remember to use the matching protocol for the script src as well. 记得也要为脚本src使用匹配协议。 The sample code above uses protocol-relative URLs which should handle most https cases properly. 上面的示例代码使用协议相对URL,它可以正确处理大多数https案例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM