2013-03-06 3 views
1

사용자가 내 index.html 페이지에서 링크를 클릭하면 페이스 북 javascript SDK를 초기화하고 로그인 상태를 확인하려고합니다. 웬일인지 이것은 작동하지 않을 것이다. jQuery가 작동하고 있으며, init 코드를 별도로 테스트 할 때 작동하지만, 함께 사용하면 작동이 멈 춥니 다. 왜 그런가?Jquery 이벤트를 사용하여 window.fbAsyncInit을 트리거하는 방법

이것은 내 index.html 페이지입니다 :

<!DOCTYPE html> 
<html> 
<head> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script> 

</head> 
<body> 
<div id="fb-root"></div> 
<script> 

이 JQuery와는 init 프로세스를 트리거하도록되어 //

$('document').ready(function(){ 
      $('a').click(function(){ 
      alert("this works"); 

    // Additional JS functions here 
    window.fbAsyncInit = function() { 
    FB.init({ 
     appId  : '39672*******', // App ID 
     channelUrl : '//http://spilot.koding.com/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 
    }); 

    // get login status 
FB.getLoginStatus(function(response) { 
    if (response.status === 'connected') { 
    // connected: redirect to start scene page 
    alert("testing"); 
    } else if (response.status === 'not_authorized') { 
    // not_authorized 
    login(); 
    } else { 
    // not_logged_in 
    login(); 
    } 
}); 
    }; 

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

// 쇼 로그인 상자

function login() { 
    FB.login(function(response) { 
     if (response.authResponse) { 
      // connected 
      alert("connected"); 
     } else { 
      // cancelled 
     } 
    }); 
} 
}); 
}); 


</script> 





<div id="findScene"><a href=""><h1>Find</h1></a></div> 
<div id="startScene"><a href=""><h1>Host</h1></a></div> 
</body> 

</html> 

답변

2

JS SDK는 비동기 적으로로드되며 전체 페이지/나머지 리소스가 있기 전에 완료 될 수 있습니다.

따라서 window.fbAsyncInit에 함수를 전달하는 것은 이미 늦은 것일 수 있습니다. $('document').ready이 실행될 때만이 작업을 수행하기 때문입니다.

$('document').ready 함수 외부의 window.fbAsyncInit에 처리기 기능을 할당하십시오.

+0

링크 중 하나에서 'javascript : window.fbAsyncInit()'을 href에 할당하려고 시도했지만 작동하지 않았습니다. 그게 $ (document) .ready 함수 밖에서 핸들러 함수를 지정한다는 의미인가요? – Spilot

+0

왜 링크를 클릭 할 때이 코드를 실행 하시겠습니까? 아니요. 가능한 한 빨리 실행하고 싶기 때문에 SDK를 사용할 때 준비가되었습니다. 단지 ready-handler의 outside_를 (n 기존의)

관련 문제