admin管理员组

文章数量:1245680

I want to use jquery/js to simulate the click on original fb share button when I click my custom button.

This is what I've got:

<script>
    $(function(){
        // initialize fb sdk
        window.fbAsyncInit = function() {
            FB.init({
              appId      : '830561340319450',
              xfbml      : true,
              version    : 'v2.2'
            });
        };

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

    });
</script>
<div class="fb-share-button" data-href="www.myweb/topic/hello_world" data-layout="button"></div>
<button class="my_own">Lets share</button>

I want when I click on my_own, the facebook stock button also get clicked. Any idea how to do that?

I want to use jquery/js to simulate the click on original fb share button when I click my custom button.

This is what I've got:

<script>
    $(function(){
        // initialize fb sdk
        window.fbAsyncInit = function() {
            FB.init({
              appId      : '830561340319450',
              xfbml      : true,
              version    : 'v2.2'
            });
        };

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

    });
</script>
<div class="fb-share-button" data-href="www.myweb./topic/hello_world" data-layout="button"></div>
<button class="my_own">Lets share</button>

I want when I click on my_own, the facebook stock button also get clicked. Any idea how to do that?

Share Improve this question edited Jul 16, 2016 at 6:04 angry kiwi asked Feb 8, 2015 at 4:26 angry kiwiangry kiwi 11.5k28 gold badges123 silver badges167 bronze badges 3
  • Check the facebook documentation for this, it lists out your options and how to do it. developers.facebook./docs/web/share – user3886234 Commented Feb 8, 2015 at 4:29
  • What option you remend in my case? – angry kiwi Commented Feb 9, 2015 at 8:23
  • Use your own button to trigger the Share dialog. developers.facebook./docs/web/share#linksharedialog – C3roe Commented Feb 9, 2015 at 9:45
Add a ment  | 

1 Answer 1

Reset to default 14 +50
  1. Remove the Facebook share button. Trying to trigger a click on it is not the way to go; it might even trigger measures they have in place to prevent click-jacking, and as a result get your app blocked by FB.

  2. Implement the call to the Share dialog when the user clicks your own button:

HTML:

<button class="my_own" type="button" onclick="share();">Lets share</button>

JavaScript:

function share() {
  FB.ui({
    method: 'share',
    href: 'http://www.myweb./topic/hello_world',
  }, function(response){});
}

本文标签: javascripthow to simulate the click event on the facebook share buttonStack Overflow