Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to simulate the click event on the facebook share button

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.net/en_US/sdk.js";
            fjs.parentNode.insertBefore(js, fjs);
        }(document, 'script', 'facebook-jssdk'));

    });
</script>
<div class="fb-share-button" data-href="www.myweb.com/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?

like image 407
angry kiwi Avatar asked Feb 08 '15 04:02

angry kiwi


People also ask

How do you trigger an event on button click?

A single click event bind to a button with an Id of “button2”. and a trigger to execute the button1 click event handler. $("#button2"). bind("click", (function () { alert("Button 2 is clicked!"); $("#button1").

How do I enable the Share button on Facebook?

Go the Page you'd like to share with your friends. Tap below the Page's cover photo. Tap Share.


1 Answers

  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.com/topic/hello_world',
  }, function(response){});
}
like image 92
CBroe Avatar answered Sep 29 '22 08:09

CBroe