javascript 用户点击 Facebook 之类的按钮后重定向

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6924371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 22:21:21  来源:igfitidea点击:

Redirect after a user clicked a facebook like button

javascriptfacebookfacebook-like

提问by stofl

I have a facebook tab with an iframe application and within that tab there is a facebook like button to have a second capability to like the facebook page. It is an ordinary facebook like button iframe.

我有一个带有 iframe 应用程序的 facebook 选项卡,在该选项卡中,有一个类似于 facebook 的按钮,可以为 Facebook 页面点赞。它是一个普通的 facebook 像按钮 iframe。

Is there a possibility to redirect the user when it clicks the button? I tried it the following code:

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

<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>

But this doesn't work. Is there a way or am I doing something wrong?

但这不起作用。有没有办法或者我做错了什么?

回答by jBit

I just tested the following code and it works as expected

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

<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>

How does it compare to your own page?

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