页面上有许多类似 facebook 的异步按钮

发布于 2024-10-16 09:19:40 字数 1962 浏览 4 评论 0原文

我正在为在我们网站上留言的人们建造一堵墙。对于每条消息,我想包含一个 Facebook Like 按钮。但 Facebook 的实现是在构建 fb:like 标签之前包含 script 标签。

 <script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script>
      <fb:like href="http://example.com/MessageWall.aspx/Fan/222" 
            show_faces="false" width="250">
 </fb:like>

我尝试过这种方法,但因为我一次在页面上显示 121 个人,所以可以说,页面性能低于标准。我不敢相信我每次都必须包含脚本标签。

我现在正在尝试实现一种异步方式来执行此操作。

我已经尝试过:

    (function() {
        var e = document.createElement('script'); e.async = true;
        e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js#xfbml=1';
            var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(e, s);
    }());

    var firstHalfOfLikeButton = '<fb:like href="http://example.com/MessageWall.aspx/Fan/';
    var secondHalfOfLikeButton = '" layout="button_count" show_faces="false" width="250"></fb:like>';
    var userId, divId;
    for (var x=0; x<listOfIdsOfActiveTiles.length; x++) {
        userId = listOfIdsOfActiveTiles[x][0];
        divId= listOfIdsOfActiveTiles[x][1];
        $("div#" + divId+ " .hbo-message").append(firstHalfOfLikeButton + userId + secondHalfOfLikeButton);
    }

我还尝试像这样将 all.js 异步附加到 fb-root,然后再循环并将 fb:like 标记附加到每个位置。

 (function() {
    var e = document.createElement('script'); e.async = true;
     e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js#xfbml=1';
         document.getElementById('fb-root').appendChild(e);
 }());

我还尝试循环并创建 fb:like 标签,然后附加 all.js 文件。

更新:在每个实例中,我都可以看到

有人有什么想法吗?

谢谢, 斯科特

更新

事后回到此页面,但我最终所做的是在单击该项目时为每个人的图像动态构建一个“喜欢”按钮。因此,我的 onclick 代码检查它是否不存在,如果不存在,则使用上面的构建块构建它。

这样,我就不必在页面加载时创建 121 个类似的按钮。相反,我按需创建它们。

您可以在 honeybunchesofoats.com 上查看完成的项目

I'm building a wall of people who've written messages on our site. For each message, I want to include a Facebook Like button. But Facebook's implementation is to include the script tag before building the fb:like tag.

 <script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script>
      <fb:like href="http://example.com/MessageWall.aspx/Fan/222" 
            show_faces="false" width="250">
 </fb:like>

I tried this approach, but because I'm showing 121 people on the page at a time, the page performance is, shall we say, sub-par. I can't believe that I have to include the script tag each time.

I'm trying now, to implement an asynchronous way of doing this.

I've tried this:

    (function() {
        var e = document.createElement('script'); e.async = true;
        e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js#xfbml=1';
            var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(e, s);
    }());

    var firstHalfOfLikeButton = '<fb:like href="http://example.com/MessageWall.aspx/Fan/';
    var secondHalfOfLikeButton = '" layout="button_count" show_faces="false" width="250"></fb:like>';
    var userId, divId;
    for (var x=0; x<listOfIdsOfActiveTiles.length; x++) {
        userId = listOfIdsOfActiveTiles[x][0];
        divId= listOfIdsOfActiveTiles[x][1];
        $("div#" + divId+ " .hbo-message").append(firstHalfOfLikeButton + userId + secondHalfOfLikeButton);
    }

I also tried to asynchronously attach the all.js to the fb-root like this BEFORE looping through and attaching the fb:like tag to each location.

 (function() {
    var e = document.createElement('script'); e.async = true;
     e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js#xfbml=1';
         document.getElementById('fb-root').appendChild(e);
 }());

I also tried looping through and creating the fb:like tag, and THEN attaching the all.js file.

Update: In each instance, I can see the <fb:like... tag is successfully created, but the facebook all.js just never goes out and actually builds the button, like it does if you load it all up at first load.

Does anyone have any ideas?

Thanks,
Scott

UPDATE

Returning to this page, way after the fact, but what I ended up doing was to dynamically build a like button for each person's image upon click of the item. So, my onclick code checks to see if it's not already there, and if not, builds it using the building blocks I have above.

This way, I don't have to create 121 like buttons upon page load. Instead, I create them on-demand.

You can see the finished project at honeybunchesofoats.com

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

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

发布评论

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

评论(1

流心雨 2024-10-23 09:19:40

您只需要包含 facebook 脚本一次,而不是每个喜欢按钮一次。然后它将在页面中搜索所有 xfbml 元素。

You only need to include the facebook script once, not once for each like button.. It will then search through the page for all xfbml elements.

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