2012-12-22 69 views
0

請幫幫我。我知道這是非常簡單的,但我的運氣不好:(使用Javascript SDK登錄後自動重定向

我試圖通過以下facebook official docs來實現facebook登錄JavaScript SDK在我的網站上,當我去mysite登錄頁面,然後facebook登錄窗口自動彈出。但我想它點擊登錄按鈕後

登錄按鈕:

<p class="fb_login">or <img src="assets/img/fb-login-button.jpg" onClick="FB.login()" onlogin="alert('Logged In');"></p> 

的Javascript:

window.fbAsyncInit = function() { 
    FB.init({ 
     appId  : '486435991400780', // App ID 
     channelUrl : '//http://localhost/ride/assets/channel.html', // Channel File 
     status  : true, // check login status 
     cookie  : true, // enable cookies to allow the server to access the session 
     xfbml  : true // parse XFBML 
    }); 

    // Additional init code here 
    FB.getLoginStatus(function(response) { 
     if (response.status === 'connected') { 
      // connected 
      replace_login(); 
      FB.api('/me', function(response) { 
       console.log('Good to see you, ' + response.name + '.'); 
       alert('Good to see you, ' + response.name + '.'); 
      }); 
     } else if (response.status === 'not_authorized') { 
      // not_authorized 
      login(); 
     } else { 
      // not_logged_in 
      login(); 
     } 
    }); 
    }; 

    function login() { 
     FB.login(function(response) { 
      if (response.authResponse) { 
       // connected 
       window.location.reload(); 
      } else { 
       // cancelled 
       alert('User cancelled login or did not fully authorize.'); 
      } 
     }); 
    } 

    function replace_login(){ 
     FB.api('/me', function(response) { 
      $('.fb_login').html('<span class="fb_logged_in">' + response.name + ', You are already logged in with Facebook!</span>'); 
     }); 
    } 

    // Load the SDK Asynchronously 
    (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 = "//connect.facebook.net/en_US/all.js"; 
     ref.parentNode.insertBefore(js, ref); 
    }(document)); 

在我的html:

<div id="fb-root"></div> 
<script src="assets/js/fb-login.js" type="text/javascript"></script> 

謝謝。

回答

2

刪除「登錄()」中的登錄狀態後的JavaScript調用進行檢查,從而使登錄彈出不上來:

// Additional init code here 
FB.getLoginStatus(function(response) { 
    if (response.status === 'connected') { 
     // connected 
     replace_login(); 
     FB.api('/me', function(response) { 
      console.log('Good to see you, ' + response.name + '.'); 
      alert('Good to see you, ' + response.name + '.'); 
     }); 
    } 
}); 

添加一個按鈕(風格與CSS最好):

<button onclick="login();">Login with Facebook</button> 

注意:如果用戶登錄(或者如果未登錄,則添加),該按鈕應該可能被javascript隱藏。

+0

是的,我明白了。因爲我已經有了onClick事件。謝謝 – itskawsar