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

发布于 2025-01-03 09:41:49 字数 1334 浏览 8 评论 0原文

我直接从 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>

I'm copying the code right out of this page on Facebook's developer website and the fbAsyncInit() method never fires. 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"

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

凉城已无爱 2025-01-10 09:41:49

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

这可能有点让人困惑。

我删除了过早加载标签定义,现在一切都好了。

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.

That probably confused things a bit.

I removed the premature load tag definition and now I am fine.

瘫痪情歌 2025-01-10 09:41:49

您应该将:更改

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

为:

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

You should change:

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

to:

 js.src = "http://connect.facebook.net/en_US/all.js";
因为看清所以看轻 2025-01-10 09:41:49

同步加载有问题吗?

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

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>
可可 2025-01-10 09:41:49

这个答案可能太晚了,在很多情况下都没有帮助,但是,我发现我的 Firefox 浏览器在经过一些体面的工作后显然变得有点疯狂并导致了那个确切的错误......只需重新启动该死的东西

This answer is probably too late and for many instances wont help but, I found out that my Firefox browser after some decent work apparently get a little crazy and cause that exact error... just restart the damn thing

追我者格杀勿论 2025-01-10 09:41:49

由于您的文件名为 test.html,并且 channelUrl 应该为 test.html,因此您正在创建一个循环引用。对于正确的channelUrl,它应该只包含一行

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

通道文件解决了跨域通信的一些问题
在某些浏览器中。 channel.html 文件的内容可以是
单行:

频道文件的缓存时间很重要
可能的。提供此文件时,您必须发送有效的 Expires 标头
有效期较长。这将确保通道文件
由浏览器缓存,这对于流畅的用户体验非常重要。
如果没有适当的缓存,跨域通信将变得非常困难
速度很慢,用户的体验会严重下降。一个简单的
在 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>

channelUrl 参数是可选的,但建议使用。提供一个
通道文件可以帮助解决三个特定的已知问题。第一的,
包含跨框架通信代码的页面可能会导致社交
没有channelUrl 时显示为空白的插件。二、如果没有
提供了channelUrl并且页面包含自动播放音频或
视频时,用户可能会听到两个音频流,因为页面有
后台二次加载跨域
沟通。第三,通道文件将防止包含额外的内容
服务器端日志中的点击次数。如果您不指定channelUrl,则
可以删除包含 fb_xd_bust 或 fb_xd_fragment 的页面视图
日志中的参数以确保正确计数。

channelUrl 必须是与页面匹配的完全限定 URL
您包含 SDK。换句话说,通道文件域
如果您的网站使用 www 提供服务,并且您修改了,则必须包含 www
您的页面上的 document.domain 必须使用相同的 document.domain
也更改channel.html 文件。协议还必须
匹配。如果您的页面通过 https 提供,则您的channelUrl 也必须是
https。请记住使用脚本 src 的匹配协议作为
出色地。上面的示例代码使用了协议相关的 URL,应该
正确处理大多数 https 情况。

Since both your file is called test.html and the channelUrl is supposed to be test.html, you're creating a circular reference. For the proper channelUrl it is supposed to just contain one line

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

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:

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

The channelUrl must be a fully qualified URL matching the page on
which you include the SDK. 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. The protocols must also
match. If your page is served over https, your channelUrl must also be
https. Remember to use the matching protocol for the script src as
well. The sample code above uses protocol-relative URLs which should
handle most https cases properly.

爱*していゐ 2025-01-10 09:41:49

请更改您的应用程序 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>

Please Change Your App ID Then Start Working

  <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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文