0

I am using

<div class="fb-share-button"  data-href="" data-layout="button_count" oncklick=""></div>

to have Facebook sharing on my page.

Now I am wondering why it is not possible to add any events to fire a function if the share button was clicked like this:

$(".fb-share-button").click(function(event){ 
//do stuff 
});

Is there any way i can achieve to add a function to the click? I want to be able to use a ajax callback to store into my database that the button was clicked, but i can not use an app id as it is a site for different customers.

Anyone with a solution?

Capsule
  • 6,118
  • 1
  • 20
  • 27
tkoatl
  • 31
  • 4
  • possible duplicate of [Facebook like and share button with callback](http://stackoverflow.com/questions/23116001/facebook-like-and-share-button-with-callback) – Capsule May 04 '15 at 04:31

1 Answers1

0

Your custom javascript won't be able to manipulate the contents of your iframe in the way that you want. Unfortunately that's the way modern browsers implement iframes.

Most common solution

Your best bet is to make a custom facebook share button that you have more control over, using facebook sharer links.

Here is some code to get you started:

<a href="https://www.facebook.com/sharer/sharer.php?u=blah.com">Share on Facebook</a>

Source: http://www.sharelinkgenerator.com/

Alternate method:

You can also link/redirect to a facebook dialog, though by the looks you need to create a facebook app, which is a pain.

https://www.facebook.com/dialog/share?
  app_id=145634995501895
  &display=popup
  &href=https%3A%2F%2Fdevelopers.facebook.com%2Fdocs%2F
  &redirect_uri=https%3A%2F%2Fdevelopers.facebook.com%2Ftools%2Fexplorer

Reference:

https://developers.facebook.com/docs/sharing/web

https://developers.facebook.com/apps

Sharn White
  • 624
  • 3
  • 15
  • Not if you use the JS SDK. Btw that's the only option you have in this case because the share is trigger by a user click, not a server-side app. – Capsule May 04 '15 at 04:39
  • 1
    Thanks. Did some further research and in combination with this: http://stackoverflow.com/questions/14829040/facebook-sharer-popup-window/14829742#14829742 it works pretty neat. – tkoatl May 04 '15 at 05:12