Friday, 15 April 2016

how to share your website in Facebook by javascript ?

 
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<!-- facebook login -->
<script>
//Load the Facebook JS SDK
     (function (d) {
        var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
        if (d.getElementById(id)) {
            return;
        }
        js = d.createElement('script');
        js.id = id;
        js.async = true;
        js.src = "https://connect.facebook.net/en_US/all.js";
        ref.parentNode.insertBefore(js, ref);
    }(document)); 
    
    
    /* (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'));
 */
// Init the SDK upon load
    window.fbAsyncInit = function () {
        FB.init({
            appId: '1459247121050170',//0
            status: true, // check login status
            cookie: true, // enable cookies to allow the server to access the session
            xfbml: true, // parse XFBML
            version: 'v2.5' // old  v2.4
        });


// Specify the extended permissions needed to view user data
// The user will be asked to grant these permissions to the app (so only pick those that are needed)
//public_profile,email
        var permissions = [
            'public_profile',
            'email',
        ].join(',');

        var fields = [
            'id',
            'name',
            'first_name',
            'middle_name',
            'last_name',
            'gender',
            'locale',
            'languages',
            'link',
            'third_party_id',
            'installed',
            'timezone',
            'updated_time',
            'verified',
            'age_range',
            'bio',
            'birthday',
            'cover',
            'currency',
            'devices',
            'education',
            'email',
            'hometown',
            'interested_in',
            'location',
            'political',
            'payment_pricepoints',
            'favorite_athletes',
            'favorite_teams',
            'picture',
            'quotes',
            'relationship_status',
            'religion',
            'significant_other',
            'video_upload_limits',
            'website',
            'work'
        ].join(',');

        function showDetails() {
            FB.api('/me', {fields: fields}, function (details) {

                alert(JSON.stringify(details));  // display all the details

                var res = jQuery.parseJSON(JSON.stringify(details));
                /* alert(res.payment_pricepoints.mobile.length);
     for(var i=0;i<res.payment_pricepoints.mobile.length;i++){   
      alert(res.payment_pricepoints.mobile[i].local_currency);
     } */
                var email = res.email;
                var firstName = res.first_name;
                var lastName = res.last_name;
                var gender = res.gender;
                var profielPictureURL = res.picture.data.url;
                //alert(profielPictureURL);

                profielPictureURL = profielPictureURL.replace(/&/g, "@");
                //alert(profielPictureURL);

                var data = "email=" + email + "&firstName=" + firstName + "&lastName=" + lastName + "&gender=" + gender + "&profielPictureURL=" + profielPictureURL + "&operation=social-media-signin&siginThrough=facebook";
                alert(data);
             /*    $.ajax({
                    type: "POST",
                    url: "LoginServlet",
                    data: data,
                    success: function (response) {
                        if (response === "0") {
                            alert("Invalid Login");
                        } else {
                            location.href = "";
                        }
                    },
                    error: function (errorMsg) {
                        alert("Error Occurred.");
                    }

                }); */

            });
        }
   


        $('#facebook').click(function () {
            //initiate OAuth Login
            FB.login(function (response) {
                // if login was successful, execute the following code
                if (response.authResponse) {
                    showDetails();
                    sharefbimage();
                }
            }, {scope: "public_profile,email"});
        });


       

        
    };
    
    //todayscode......................
    function sharefbimage() {
  FB.init({ appId: '1459247121050170', status: true, cookie: true });
  FB.ui(
  {
              method: 'share',
              name: 'Facebook Dialogs',
              href: $(location).attr('href'),
              link: 'www.plateofcode.com',
              picture: 'https://www.blogger.com/img/blogger-logo-small.png',
              caption: ' Book',
              description: 'description'
          },
          function (response) {
           alert(JSON.stringify(response));
              if (response && response.post_id) {
                  alert('success');
              } else {
                 alert('error');
              }
          }
        );
    }
    //

</script>

<!-- end facebook login -->



</head>
<body>
<button id="facebook" style="background-color: #0967DA;color:white;border: thin;">facebook</button>

</body>
</html>
 
















It will work as....


No comments:

Post a Comment