Monday, October 15, 2012

Facebook Share Button for your web

Hi, I was curious on a simple code which create smart buttons to publish your web link to your particular social web account. Finally I could do that with these codes.

Share a link on Facebook with an image as a thumbnail picture.

Here you can create a facebook app for web pages where you are issued a facebook app id and a app secret where you can use some magnificent  services provided by the (REST) service of facebook.

  • Log on to your facebook account and go here.
  • Click Create New App
  • Give a name for your app at App Name and click Continue
  • Click "Web site with facebook login" and give the domain name for the site url. (It accepts domain names like http://localhost/ for development purposes.)
  • Click "Save Changes".
  • Record the App Id and the App Secret.
  • Give a fine picture to use as the thumbnail image for your app
Javascript codes for opening a pop up for sharing a link with an image for thumbnail picture for your link is as follow.
<script>
function fb_share(url, im_url, name, caption, description, redirect_url){
     var windowWidth=750;
     var windowHeight=500;
     var centerWidth = (window.screen.width - windowWidth) / 2;
     var centerHeight = (window.screen.height - windowHeight) / 2;
var share='http://www.facebook.com/dialog/feed?app_id='+fb_app_id+'&link='+url+'&picture='+im_url+'&name='+name+'&caption='+caption+'&description='+description+'&redirect_uri='+redirect_url; 
var newWindow = window.open(share,'fbshare','resizable=0,width=' + windowWidth + ',height=' +windowHeight +',left=' + centerWidth +',top=' + centerHeight);
             newWindow.focus();
             return false;
}
</script>

There is another way I have experienced to share a link on your time line. It does not need creating a facebook app. Here is the jvascript code.
<scipt type="text/javascript">
function fb_share(title, summary, sharing_url){

     var windowWidth=750;
     var windowHeight=500;
     var centerWidth = (window.screen.width - windowWidth) / 2;
     var centerHeight = (window.screen.height - windowHeight) / 2;



var newWindow = window.open('http://www.facebook.com/sharer.php?s=100&p[title]='+title+'&p[summary]='+summary+'&p[url]='+sharing_url,'fbshare','resizable=0,width=' + windowWidth +
                ',height=' + windowHeight +
                ',left=' + centerWidth +
                ',top=' + centerHeight);
            newWindow.focus();
            return false;

}
</script>

Here the url should be url encoded. return false will stop the parent window refreshing once the pop up window is opened.

















No comments:

Post a Comment