javascript 带有新 Facebook 分享按钮的回调函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20312964/
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-27 18:13:15  来源:igfitidea点击:

Callback function with new Facebook Share button

javascriptfacebookfacebook-javascript-sdkfacebook-eventsfacebook-widgets

提问by Gabriele Perego

I'm trying to subscribe to the "edge.create" event in conjunction with new Facebook Share button without any luck. I'm using the standard event subscription:

我正在尝试与新的 Facebook 共享按钮一起订阅“edge.create”事件,但没有任何运气。我正在使用标准事件订阅:

FB.Event.subscribe('edge.create',
    function(response) {
        console.log(response);
    }
);

The event is not fired... It works instead with the Like button

该事件不会被触发...它与“赞”按钮一起工作

Any suggestion?

有什么建议吗?

回答by JamesIngold

The facebook share button has been deprecated in favor of the like button. From their docs:

facebook 分享按钮已被弃用,取而代之的是喜欢按钮。从他们的文档:

The Share button has been deprecated in favor of the Like button, and will no longer be supported. Please use the Like button whenever possible to drive maximum traffic to your apps.

Share 按钮已被弃用,取而代之的是 Like 按钮,并且将不再受支持。请尽可能使用“赞”按钮来为您的应用带来最大流量。

You can use the event with the like button, however if you want to track the shared button then it is still possible with some javascript. You can add a click event to your FB share button.

您可以将事件与喜欢按钮一起使用,但是如果您想跟踪共享按钮,那么仍然可以使用一些 javascript。您可以将点击事件添加到您的 FB 分享按钮。

Here is an example I'm using.

这是我正在使用的示例。

window.fbAsyncInit = function () {
    FB.init({
        appId: 'your app id',
        status: true,
        cookie: true,
        xfbml: true,
        oauth: true
    });

    $('#facebook-share').click(function () {
        FB.ui({
            method: 'feed',
            link: document.URL,
            caption: 'example',
        }, function (response) {
            if (response === null) {
                console.log('post was not shared');
            } else {
                console.log('post was shared');
            }
        });
    });
};

(function (d, s, id) {
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) return;
    js = d.createElement(s); js.id = id;
    js.async = true;
    js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
    fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));

回答by Tiago Gouvêa

Create a facebook app and get approved are boring and can take so much time...

创建一个 facebook 应用程序并获得批准很无聊,而且可能需要很多时间......

An alternative way I made here to who don't want to publish an APP just for it is:

我在这里为不想发布应用程序的人提供的另一种方法是:

   jQuery("#div_share").click(function() {

        var url = "http://www.facebook.com/sharer/sharer.php?u=YOUR_URL&title=YOUR_TITLE";

        var openDialog = function(uri, name, options, closeCallback) {
            var win = window.open(uri, name, options);
            var interval = window.setInterval(function() {
                try {
                    if (win == null || win.closed) {
                        window.clearInterval(interval);
                        closeCallback(win);
                    }
                }
                catch (e) {
                }
            }, 1000);
            return win;
        };

        openDialog(url,'Facebook','width=640,height=580', function(){
            jQuery("#div_share").hide();
            jQuery("#formulario").show();
        });
    });

It will open a javascript dialog to share, and know when the user close it. It's not guaranteed that they really share the content... but.. :)

它将打开一个 javascript 对话框进行共享,并知道用户何时关闭它。不能保证他们真的分享内容......但是...... :)