如何添加 Facebook“喜欢"按钮到 AJAX 驱动的页面 [英] How to add a Facebook "Like" button to an AJAX driven page

查看:30
本文介绍了如何添加 Facebook“喜欢"按钮到 AJAX 驱动的页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索了网络和 Stack Overflow,但没有找到这个问题的适当答案.在我开始寻找自己的解决方案的试错过程之前,我想我会求助于 Stack Overflow 智囊团,看看是否已经有一个成功的实施.

我有一个由 AJAX 驱动的页面,对于非 JavaScript 浏览器和 SEO,它会正确降级.AJAX 版本中的每次点击都可以由一个唯一的 URL 表示.

我想做的是动态改变按钮的HREF.我知道这个标签在运行时被转换为标准 HTML(即转换成一个令人讨厌的表格/iframe 布局).

我只是想知道是否有人对如何在 AJAX 驱动的页面上实现这个类似 FB 的按钮有任何见解?

提前干杯:)

你觉得我刚刚一起破解的这个方法怎么样?看到它有什么大问题了吗?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="服务器"><title></title><script src="JS/jquery/jquery.js" type="text/javascript"></script><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><script language="javascript" type="text/javascript">$("document").ready(功能 (){CreateNewLikeButton("http://www.yahoo.com")$("a#ChangeToGoogle").点击(功能(e){e.preventDefault();CreateNewLikeButton("http://www.google.ca")});});函数 CreateNewLikeButton(url){var elem = $(document.createElement("fb:like"));elem.attr("href", url);$("div#Container").empty().append(elem);FB.XFBML.parse($("div#Container").get(0));}<身体><form id="form1" runat="server"><a id="ChangeToGoogle" href="#">更改为 Google</a><div id="容器"><fb:like href="http://www.NEVER_LINK_TO_THIS_12345.com"></fb:like>

</表单>

解决方案

SIMPLE SOLUTION

当加载完成时,只需解析触发解析功能.

如果您使用的是 jQuery,那么这个问题有一个真正简单而巧妙的解决方案:

$(document).ajaxComplete(function(){尝试{FB.XFBML.parse();}catch(ex){}});

http://developers.facebook.com/docs/reference/plugins/like/

I have trawled the net and Stack Overflow and have not found an adequate answer to this question. Before I start the trial and error process of finding my own solution, I thought I would turn to the Stack Overflow braintrust and see if there was already a successful implementation.

I have an AJAX powered page that degrades properly for non-javascript browsers and SEO. Each click in the AJAX version can be represented by a unique URL.

What I want to do is to dynamically change the HREF of the button. I do understand that this tag is converted to standard HTML at runtime (namely into a nasty table / iframe layout).

I was just wondering if anyone had any insight as to how to implement this FB like button onto AJAX powered pages?

Cheers in advance :)

EDIT:

What do you think of this method I just hacked together? See any huge problems with it?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

    <script src="JS/jquery/jquery.js" type="text/javascript"></script>
    <script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script>

    <script language="javascript" type="text/javascript">
        $("document").ready
        (
            function ()
            {
                CreateNewLikeButton("http://www.yahoo.com")

                $("a#ChangeToGoogle").click
                (
                    function (e)
                    {
                        e.preventDefault();
                        CreateNewLikeButton("http://www.google.ca")
                    }
                );

            }
        );

        function CreateNewLikeButton(url)
        {
            var elem = $(document.createElement("fb:like"));
            elem.attr("href", url);
            $("div#Container").empty().append(elem);
            FB.XFBML.parse($("div#Container").get(0));
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <a id="ChangeToGoogle" href="#">Change To Google</a>
    <div id="Container">
        <fb:like href="http://www.NEVER_LINK_TO_THIS_12345.com"></fb:like>
    </div>
    </form>
</body>

</html>

解决方案

SIMPLE SOLUTION

Just parse trigger the parse function when load complete.

If you’re using jQuery, there’s a real easy and slick solution to this problem:

$(document).ajaxComplete(function(){
    try{
        FB.XFBML.parse(); 
    }catch(ex){}
});

http://developers.facebook.com/docs/reference/plugins/like/

这篇关于如何添加 Facebook“喜欢"按钮到 AJAX 驱动的页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆