如何添加Facebook和QUOT;像"按钮,一个AJAX驱动的页面 [英] How to add a Facebook "Like" button to an AJAX driven page

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

问题描述

我已经拖网网和堆栈溢出,并没有发现充分的回答这个问题。在我开始寻找自己的解决方案的试错过程中,我想我会谈谈堆栈溢出智囊团,看看是否有已经是一个成功的实施。

我有一个AJAX供电的页面,正确地降低非JavaScript的浏览器和搜索引擎优化。在AJAX版本,每一次点击可以重新通过一个唯一的URL psented $ P $。

我想要做的就是动态地改变按键的HREF。我也明白,这个标签转换成标准的HTML在运行时(即进入一个讨厌的表/ IFRAME布局)。

我只是想知道如果任何人有任何见解,如何实现这个FB像按钮拖到AJAX供电的网页?

提前干杯:)

编辑:

你觉得这个方法我砍死在一起的?见任何巨大的问题呢?

 <!DOCTYPE HTML PUBLIC -  // W3C // DTD XHTML 1.0过渡// ENhttp://www.w3.org/TR/xhtml1/DTD/ XHTML1-transitional.dtd>

< HTML的xmlns =htt​​p://www.w3.org/1999/xhtml>
<头=服务器>
    <冠军>< /标题>

    <脚本SRC =JS / jQuery的/ jquery.js和类型=文/ JavaScript的>< / SCRIPT>
    &所述;脚本的src =htt​​p://connect.facebook.net/en_US/all.js#xfbml=1>&所述; /脚本>

    < SCRIPT LANGUAGE =JavaScript的类型=文/ JavaScript的>
        $(文件)。就绪
        (
            功能 ()
            {
                CreateNewLikeButton(http://www.yahoo.com)

                $(A#ChangeToGoogle)。点击
                (
                    功能(五)
                    {
                        即preventDefault();
                        CreateNewLikeButton(http://www.google.ca)
                    }
                );

            }
        );

        功能CreateNewLikeButton(URL)
        {
            VAR的elem = $(document.createElement方法(FB:像));
            elem.attr(HREF,网址);
            。$(#DIV容器)空()追加(ELEM);
            FB.XFBML.parse($(#DIV容器)获得(0));
        }
    < / SCRIPT>
< /头>
<身体GT;
    <表格ID =Form1的=服务器>
    <一个ID =ChangeToGoogle的href =#>更改为谷歌< / A>
    < D​​IV ID =容器>
        < FB:像HREF =htt​​p://www.NEVER_LINK_TO_THIS_12345.com>< / FB:等>
    < / DIV>
    < /形式GT;
< /身体GT;

< / HTML>
 

解决方案

简单的解决方案

只是解析:当载荷完成触发解析功能。

如果你正在使用jQuery,有一个真正的轻松和漂亮的解决了这个问题:

  $(文件).ajaxComplete(函数(){
    尝试{
        FB.XFBML.parse();
    }赶上(前){}
});
 

<一个href="http://developers.facebook.com/docs/reference/plugins/like/">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和QUOT;像&QUOT;按钮,一个AJAX驱动的页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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