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

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

问题描述

我已经拖网和堆栈溢出,并没有找到足够的答案这个问题。在我开始尝试和错误的过程找到我自己的解决方案之前,我以为我会转向Stack Overflow braintrust,看看是否已经有一个成功的实现。



我有一个AJAX供电的页面,为非JavaScript浏览器和SEO正确降级。 AJAX版本中的每次点击都可以由唯一的网址表示。



我想做的是动态更改按钮的HREF。我明白这个标签在运行时被转换为标准的HTML(即是一个讨厌的表/ iframe布局)。



我只是想知道有没有人有任何洞察力如何在AJAX电源页面上实现这样的FB按钮?



提前提醒:)



编辑:



你觉得这个方法我刚刚被黑客入侵?看到任何巨大的问题吗?

 <!DOCTYPE html PUBLIC -  // W3C // DTD XHTML 1.0 Transitional // ENhttp://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.jstype =text / javascript>< / script>
< script src =http://connect.facebook.net/en_US/all.js#xfbml=1>< / script>

< script language =javascripttype =text / javascript>
$(document)。ready

function()
{
CreateNewLikeButton(http://www.yahoo.com)

$(a#ChangeToGoogle)点击

function(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));
}
< / script>
< / head>
< body>
< form id =form1runat =server>
< a id =ChangeToGooglehref =#>更改为Google< / a>
< div id =Container>
< fb:like href =http://www.NEVER_LINK_TO_THIS_12345.com>< / fb:like>
< / div>
< / form>
< / body>

< / html>


解决方案

SIMPLE SOLUTION



加载完成时,只需解析触发解析功能即可。



如果您使用的是jQuery,那么这个问题有一个非常简单和轻松的解决方案:

  $(document).ajaxComplete(function(){
try {
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“Like”按钮到AJAX驱动页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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