April 29, 2012

Click event of Facebook like button

I have embedded Facebook like button in my web application.
Is there any method we can capture event on like button click?
Yes we can do it with Facebook JavaScript SDK.

ASP.Net Code Snippet


 
<script type="text/javascript">
    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/all.js#xfbml=1";
        fjs.parentNode.insertBefore(js, fjs);
        }
        (document, 'script', 'facebook-jssdk'));
    </script>
    <form id="form2" runat="server">
    <div>
        <div id="fb-root">
        </div>
        <fb:like href="http://shatrughnadiary.blogspot.in" send="false" width="450" show_faces="false">
        </fb:like>
        <script src="http://connect.facebook.net/en_US/all.js"></script>
        <script>
            FB.init({
                status: true, // check login status               
                cookie: true, // enable cookies to allow the server to access the session
                xfbml: true  // parse XFBML
            });       
        </script>
        <script type="text/javascript">
            FB.Event.subscribe('edge.create', function (response) {
                alert('You liked the URL: ' + response);
            });        
    </script
>

This is working sample.

That’s It.
Enjoy Learning.

No comments:

Post a Comment