简体   繁体   中英

facebook login and like button javascript conflicting

This Is Login javascript used here

<script> logInWithFacebook = function() {
FB.login(function(response) {
  if (response.authResponse) {
    alert('You are logged in & cookie set!');
    // Now you can redirect the user or do an AJAX request to
    // a PHP script that grabs the signed request from the cookie.
  } else {
    alert('User cancelled login or did not fully authorize.');
  }
});
return false;
};
 window.fbAsyncInit = function() {
FB.init({
  appId: '1166049000075578',
  cookie: true, // This is important, it's not enabled by default
  version: 'v2.4'
});
};

(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'));
//Onclick for fb login
jQuery(document).ready(function($) {
$('#facebook1').click(function(e) {
FB.login(function(response) {
  if(response.authResponse) {
      parent.location ='Someurl'; //redirect uri after closing the facebook popup
  }
 },{scope:'email,public_profile,user_friends'}); //permissions for facebook
 });

 });</script>

Now this is the facbook Like button script used

<script>(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#xfbml=1&version=v2.4&appId=1166049000075578   ";
 fjs.parentNode.insertBefore(js, fjs);
 }(document, 'script', 'facebook-jssdk'));</script>

The login script and the like button script both needs to be present on the same page if am only using like button javacript and html code then is working fine
But using both script( login and like ) on a same page creating problem
The problem started when I upgraded to Facebook SDKv5
Website where it is used

If you look at the code you'll find that the code snipped you named "facbook Like button script" is just a subsection of the other code snipped you named "Login javascript".

That means you can just leave out the <script> section of the code you need to add for the like button if you already have the code for Facebook login working.

This set of code is repeated twice

<script>(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#xfbml=1&version=v2.4&appId=1166049000075578   ";
fjs.parentNode.insertBefore(js, fjs);
(document, 'script', 'facebook-jssdk'));</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM