将类似 facebook 的按钮与动态加载的内容集成

发布于 2024-10-26 17:13:34 字数 277 浏览 2 评论 0 原文

我正在开发的网站包含通过 AJAX 动态加载的项目列表。当您向下滚动页面时,会加载更多项目。

现在,我的客户想要为每个项目添加一个类似 Facebook 的按钮(以及喜欢此按钮的人数)。

集成默认的点赞按钮没有问题,但是如何将点赞按钮添加到通过 AJAX 加载的项目中?如果加载一个新项目,Facebook API 如何知道 DOM 树中有一个新的点赞按钮,需要获取有多少人点赞该按钮?

有没有人有如何做到这一点的经验?有可用的例子吗?除了集成标准的按钮之外,我的搜索没有发现任何有用的东西。

The website I am working on consists of a list of items dynamically loaded via AJAX. As you scroll down the page, more items are loaded.

Now my customer wants to add a Facebook like button (and the number of people who liked this) to each of these items.

Integrating the default like button is no problem, but how do I add the like button to the items loaded via AJAX? If a new item is loaded, how does the Facebook API know that there is a new like button in the DOM tree for which it needs to fetch the count of how many people liked this?

Has anyone experience in how to do this? Is there an example available? My search didnt turn up anything usefull except integrating the standard like button.

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

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

发布评论

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

评论(4

夏至、离别 2024-11-02 17:13:34

迟到的答案,但您可以在函数的回调中使用 Facebook API 的解析函数,该函数加载新元素以呈现新的类似按钮:

FB.XFBML.parse();

您还可以将解析器定向到您想要呈现的元素,因此它不会重新渲染已经存在的类似按钮:

FB.XFBML.parse(document.getElementById('foo'));

这直接来自文档:https://developers .facebook.com/docs/reference/javascript/FB.XFBML.parse/

Late answer, but you could use the parse function of the Facebook API in a callback of your function that loads the new elements to render the new like buttons:

FB.XFBML.parse();

You can also direct the parser to the elements you want rendered, so it doesn't re-render the already existing like buttons:

FB.XFBML.parse(document.getElementById('foo'));

This is straight from the docs: https://developers.facebook.com/docs/reference/javascript/FB.XFBML.parse/

离去的眼神 2024-11-02 17:13:34

迟到的答案,但以防万一有人需要它。

刚刚完成了我的项目,几乎与所描述的项目相同。
我的页面通过 ajax 获取 json 格式的帖子,然后我创建 dom 元素,包括 facebook like 按钮、tweet 按钮和 google plus one 按钮。在我解决问题之前,我遇到了很多问题。主要问题是,类似的按钮一次又一次无法按预期工作......之后我找到了有效的解决方案。

我正在使用 facebook js sdk (您可以找到一些有用的信息这里

<div id="fb-root"></div>
<script>
  var isFBLoaded = false;
  window.fbAsyncInit = function() {
    FB.init({
        appId: ' your api key', 
        status: true, 
        cookie: true, 
        xfbml: false
    });
    isFBLoaded = true;
    ...
    renderFBqueue(); // i ll explain this later
  };
  (function() {
    var e = document.createElement('script'); 
    e.async = true;
    e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
    document.getElementById('fb-root').appendChild(e);
  }());
</script>

似乎ajax内容和FB.XFBML.parse() 方法或其他方法。
我在 fb 开发者论坛 上找到了解决方案,并对其进行了修改以适合我的情况需要。因此,在我从 ajax 调用获取 json 内容(多个帖子)后,我使用 jquery 创建除 fb 之类按钮之外的所有元素。我将帖子 url(以及其他一些相关数据)放入队列中,并使用超时调用函数来创建/解析这些按钮。

var fb_queue = [];
...
function getMorePosts(){
    $.get('moreposts.php', params, function(response){
        if (response) createPosts(response);
    }, 'json');    
}
...
function createPosts(data){
    ...
    for (var key in data.posts) {
        ...
        fb_queue.push({ id : data.posts[key].id , url : data.posts[key].url });
    }
    ... // elements are created

}

最后创建和解析 fb 像按钮

function parseFBqueue(){
    if(isFBLoaded){
        if (fb_queue.length==0) return false;
        $.each(fb_queue, function (j, data) {
            window.setTimeout(function () { 
                renderFBLike(fb_queue.shift());  
            }, (j + 1) * 300); 
        });  
    };
}
function renderFBLike(data){
    var fblike = '<fb:like href="'+data.url+'" layout="button_count" ... ></fb:like>'
    $('#fbdiv-'+data.id).append(fblike);
    FB.XFBML.parse(document.getElementById('fbdiv-'+data.id'));    
}

希望有人会发现这很有用,我知道一周前我会的:)

Late late answer but just in case someone need it.

Just finished my project nearly same as the one described.
My page gets posts via ajax in json format, and i than create dom elements including facebook like button, tweet button and google plus one button. I ve had lots of problems until i get it right. Major problem was that like buttons didnt work as expected again and again... After while i found working solution.

I am using facebook js sdk (you can find some useful info here)

<div id="fb-root"></div>
<script>
  var isFBLoaded = false;
  window.fbAsyncInit = function() {
    FB.init({
        appId: ' your api key', 
        status: true, 
        cookie: true, 
        xfbml: false
    });
    isFBLoaded = true;
    ...
    renderFBqueue(); // i ll explain this later
  };
  (function() {
    var e = document.createElement('script'); 
    e.async = true;
    e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
    document.getElementById('fb-root').appendChild(e);
  }());
</script>

It seems that there is a problem whit ajax content and FB.XFBML.parse() method or something.
I found a solution on fb developers forum, and modified it to fit my needs. So after i get json content (multiple posts) from ajax call, i create all elements with jquery except fb like buttons. I put the post url (and some other related data) in queue and call function with timeout to create/parse those buttons.

var fb_queue = [];
...
function getMorePosts(){
    $.get('moreposts.php', params, function(response){
        if (response) createPosts(response);
    }, 'json');    
}
...
function createPosts(data){
    ...
    for (var key in data.posts) {
        ...
        fb_queue.push({ id : data.posts[key].id , url : data.posts[key].url });
    }
    ... // elements are created

}

and finaly to create and parse fb like buttons

function parseFBqueue(){
    if(isFBLoaded){
        if (fb_queue.length==0) return false;
        $.each(fb_queue, function (j, data) {
            window.setTimeout(function () { 
                renderFBLike(fb_queue.shift());  
            }, (j + 1) * 300); 
        });  
    };
}
function renderFBLike(data){
    var fblike = '<fb:like href="'+data.url+'" layout="button_count" ... ></fb:like>'
    $('#fbdiv-'+data.id).append(fblike);
    FB.XFBML.parse(document.getElementById('fbdiv-'+data.id'));    
}

Hope someone will find this useful, I know that I would a week ago :)

恋你朝朝暮暮 2024-11-02 17:13:34

Facebook 点赞按钮只是页面上的一个

<iframe src="http://www.facebook.com/plugins/like.php?href=YOUR_URL"
        scrolling="no" frameborder="0"
        style="border:none; width:450px; height:80px"></iframe>

因此,本质上,您需要做的就是将新的

The Facebook like button is just an <iframe> on the page. There is no "Facebook API" running, as such. The like count is part of the content inside of the <iframe>, and gets loaded with the rest of the content when the src URL is loaded by the browser. The code for a like button goes like:

<iframe src="http://www.facebook.com/plugins/like.php?href=YOUR_URL"
        scrolling="no" frameborder="0"
        style="border:none; width:450px; height:80px"></iframe>

So in essence, all you should need to do is add a new <iframe> to the page in the correct location and with the correct src URL, and all the rest will be handled automatically for you.

淑女气质 2024-11-02 17:13:34

ajax 完成后调用此 javascript 函数;)

fb_sharepro_render()

call this javascript function after ajax complete ;)

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