Why is FB.Event.subscribe firing?

2020-07-23 07:30发布

问题:

What causes the FB.Event.subscribe event to fire?

I know clicking an fb:login-button element can trigger it but the FB.Event.subscribe event is firing anytime a Facebook session is created.

Do other FBML elements automatically trigger this event when a Facebook session is present or does this event trigger regardless of regardless of any FBML elements present?

回答1:

FB.Event.subscribe by itself is not an event, it's just like the addListener where it hook an event to a function. You need to check your code to see what events you are using:

  • auth.login -- fired when the user logs in
  • auth.logout -- fired when the user logs out
  • auth.sessionChange -- fired when the session changes
  • auth.statusChange -- fired when the status changes
  • xfbml.render -- fired when a call to FB.XFBML.parse() completes
  • edge.create -- fired when the user likes something (fb:like)
  • edge.remove -- fired when the user unlikes something (fb:like)
  • comments.create -- fired when the user adds a comment (fb:comments)
  • comments.remove -- fired when the user removes a comment (fb:comments)
  • fb.log -- fired on log message

I suppose you have the auth.sessionChange event used.



回答2:

I had this same issue, but the solution I found was:

  1. In FB.init, set the parameter status to false (in almost all samples I saw it comes as true by default)
  2. Trigger the login events using the auth.statusChange

So you'll end with something like it:

<script>
    window.fbAsyncInit = function () {
        FB.init({
            appId: 'YOUR-APPID',
            channelUrl: '//www.YOUR-DOMAIN.com/channel.html',
            status: false,
            cookie: true,
            xfbml: true
        });

        FB.Event.subscribe("auth.statusChange", function (response) {
            if (response.authResponse)
            {
                if (FBLoginCallback) FBLoginCallback();
            }
        });
    };

    (function (d) {
        var js, id = 'facebook-jssdk'; if (d.getElementById(id)) { return; }
        js = d.createElement('script'); js.id = id; js.async = true;
        js.src = "//connect.facebook.net/pt_BR/all.js";
        d.getElementsByTagName('head')[0].appendChild(js);
    } (document));
</script>

Notice that I made a callback function named FBLoginCallback(), so I can use this code in a MasterPage, and define the callback handling function in separated pages, each with its specific needs.



回答3:

You can only know what triggers each event by looking at the Facebook docs or by looking through the JS SDK source code. Here are links for both of those:

  • https://developers.facebook.com/docs/reference/javascript/FB.Event.subscribe/#events
  • https://github.com/fashy/fbtracking/blob/master/js-sdk/all.beautified.js

In the JS source, look for calls to FB.Event.fire( ... ). You may have to do some more analysis to figure out the exact conditions, especially since my version of the JS is from a minified version of the SDK. Facebook does not release their actual source at this time so we have to make do with de-minifying the final SDK.



标签: facebook