2012-11-22 6 views
3

내 웹 사이트에이 코드가 있습니다. 내 페이지가로드되고 사용자가 앵커 태그를 클릭하지 않은 경우 Facebook 로그인 대화 상자가 표시됩니다. Facebook 로그인 onClick - 자바 스크립트

<script> 
    // Additional JS functions here 
    window.fbAsyncInit = function() { 
    FB.init({ 
     appId  : 'xxxxxxxxxxxxxx', // App ID 
     channelUrl : '//192.168.1.127/test/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 
     } else if (response.status === 'not_authorized') { 
     // not_authorized 
     login(); 
     } else { 
     // not_logged_in 
     login(); 
     } 
    }); 
    }; 

    function login() { 
    FB.login(function(response) { 
     if (response.authResponse) { 
     testAPI() ; 
     } else { 
     // cancelled 
     } 
    }); 
    } 

    function testAPI() { 
    console.log('Welcome! Fetching your information.... '); 
    FB.api('/me', function(response) { 
     console.log('Good to see you, ' + response.name + '.'); 
    }); 
    } 

    // 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)); 
</script> 

은 내가 FB.getLoginStatus 이벤트가 해고되고 싶지 때마다이에 사용자가 클릭 :

<a href = "#" onClick = "FB.getLoginStatus()">Login</a> 

하지만 지금은 그것이 나를 anythings를하지 않고로드합니다.

제안 사항?

답변

8

좋아요. 여기에 간단한 해결책이 있습니다. 다른 기능에서 FB.getLoginStatus으로 전화를 걸고 단추를 기능을 사용하는 으로 호출하도록합니다.

function doLogin(){ 
    FB.getLoginStatus(function(response) { 
    ... 
    }; 
} 

그럼 당신은 doLogin()에 버튼에 onClick을 변경할 수 있습니다.

Bassicaly, Facebook SDK가로드되자 마자 FB.getLoginStatus 코드가 포함 된 window.fbAsyncInit 함수를 호출하므로 SDK 준비가 완료되면 로그인 코드가 실행됩니다.

관련 문제