Facebook javascript firing on a “share”

后端 未结 2 1875
谎友^
谎友^ 2021-01-06 23:50

I saw this link here:

How to detect Facebook share success? with Javascript

But how do I implement that?

相关标签:
2条回答
  • 2021-01-07 00:25

    First you need to have the Javascript SDK loaded in your page

    <div id="fb-root"></div>
    <script>
      window.fbAsyncInit = function() {
        FB.init({
          appId      : 'YOUR_APP_ID', // App ID
          status     : true, // check login status
          cookie     : true, // enable cookies to allow the server to access the session
          oauth      : true, // enable OAuth 2.0
          xfbml      : true  // parse XFBML
        });    
    
      };
    
      // Load the SDK Asynchronously
      (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/en_US/all.js";
         d.getElementsByTagName('head')[0].appendChild(js);
       }(document));
    </script>
    

    Next you have a function that contains the FB.ui code for opening the share dialog. Within the FB.ui function you can see where the callback starts function(response) {, where 'response' contains some details that help you determine if the user did share the message.

    In the callback we do an IF statement. If the user did post the message response.post_id exists and contains the id of the successfully posted message so then we can do whatever we want, in this example an alert pops up saying 'Post was published'

    function share(){
      FB.ui(
        {
          method: 'feed',
          name: 'Facebook Dialogs',
          link: 'http://developers.facebook.com/docs/reference/dialogs/',
          picture: 'http://fbrell.com/f8.jpg',
          caption: 'Reference Documentation',
          description: 'Dialogs provide a simple, consistent interface for applications to interface with users.',
          message: 'Facebook Dialogs are easy!'
        },
    
        function(response) {
          if (response && response.post_id) {
    
            // THE POST WAS PUBLISHED
            alert('Post was published.');
    
          } else {
    
            // THE POST WAS NOT PUBLISHED
            alert('Post was not published.');
    
          }
        }
      );
    }
    
    0 讨论(0)
  • 2021-01-07 00:41

    Here you have instructions how to init FB Javascript SDK, then use function from your's link.

    0 讨论(0)
提交回复
热议问题