2012-04-26 73 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 2012-04-26 18:43:55

0

這是因爲在showAlertreturn沒有返回 「分爲」 pageFan功能。 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

好吧,我明白了,但是解決方案是什麼? – 2012-04-26 17:41:25

+0

解決方法是修改'showAlert'中的'debug'元素。編輯答案 – 2012-04-26 18:15:02