在用户点击类似Facebook的按钮后重定向

时间:2011-08-03 09:31:45

标签: javascript facebook facebook-like

我有一个带有iframe应用程序的facebook标签,并且在该标签内有一个类似Facebook的按钮,可以获得第二个喜欢facebook页面的功能。这是一个普通的facebook if button iframe。

点击按钮时是否有可能重定向用户?我尝试了以下代码:

<div id="fb-root"></div>
<script type="text/javascript">
    window.fbAsyncInit = function() {
        FB.init({appId: '<my app id>', status: true, cookie: true, xfbml: true});
        FB.Canvas.setSize({ width: 520, height: 1500 });
        FB.Event.subscribe('edge.create',
            function(response) {
                alert('like!');
            }
        );
    };

    //Load the SDK asynchronously
    (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>

但这不起作用。有办法还是我做错了什么?

1 个答案:

答案 0 :(得分:1)

我刚刚测试了以下代码,它按预期工作

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">
<head></head>
<body>
<div id="fb-root"></div>

<fb:like href="http://yoururlto.like" send="false" width="450" show_faces="false" font=""></fb:like>

<script type="text/javascript">
    window.fbAsyncInit = function() {
        FB.init({appId: 'YOUR_APP_ID', status: true, cookie: true, xfbml: true});
        FB.Canvas.setSize({ width: 520, height: 1500 });
        FB.Event.subscribe('edge.create',
            function(response) {
                alert('like!');
                // put redirect code here eg
                window.location = "http://www.mysite.com/redirected.html"; 
            }
        );
    };

    //Load the SDK asynchronously
    (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>
</body>
</html>

它与您自己的页面相比如何?