Facebook javascript firing on a “share”

后端 未结 2 1951
谎友^
谎友^ 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

    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.');
    
          }
        }
      );
    }
    

提交回复
热议问题