2012-04-26 9 views
2

ログインした後、ユーザーがFacebookページのファンではないが結果が常に「未定義」である場合に返信しようとしています。しかし、「リターン」を「アラート」に置き換えると、完全に機能します。ユーザーがFacebookページのファンであることを確認

function pageFan() 
{ 
    FB.api({ method: 'pages.isFan', page_id: '175625039138809' }, function(response) { 
     showAlert(response); 
    }); 
} 

function showAlert(response) 
{ 
    if (response == true) { 
     return 'like the Application.'; 
    } else { 
     return "doesn't like the Application."; 
    } 
} 

var like = pageFan(); 
document.getElementById('debug').innerHTML = like; //return undefined 

答えて

1

この質問はすでにbeen answeredです。

関連Javascriptを:

$(document).ready(function(){ 
     FB.login(function(response) { 
     if (response.session) { 

      var user_id = response.session.uid; 
      var page_id = "40796308305"; //coca cola 
      var fql_query = "SELECT uid FROM page_fan WHERE page_id = "+page_id+"and uid="+user_id; 
      var the_query = FB.Data.query(fql_query); 

      the_query.wait(function(rows) { 

       if (rows.length == 1 && rows[0].uid == user_id) { 
        $("#container_like").show(); 

        //here you could also do some ajax and get the content for a "liker" instead of simply showing a hidden div in the page. 

       } else { 
        $("#container_notlike").show(); 
        //and here you could get the content for a non liker in ajax... 
       } 
      }); 


     } else { 
     // user is not logged in 
     } 
    }); 
+0

response.sessionは古くなっていますが、正しい方法は次のとおりです。response.authResponse – Philip

0

showAlertreturnpageFan機能 "に" 帰国されていないためです。 showAlert関数はコールバックとして渡されます。つまり、pageFanの実行の外側で後でコールされることを意味します。私はあなたがmore about callback functions and asynchronous programmingを読む必要があると思います。

function showAlert(response) 
{ 
    if (response == true) { 
     document.getElementById('debug').innerHTML = 'like the Application.'; 
    } else { 
     document.getElementById('debug').innerHTML = "doesn't like the Application."; 
    } 
} 
+0

わかりましたが、解決策は何ですか? –

+0

解決方法は 'showAlert'内の' debug'要素を修正することです。編集された答え –

関連する問題