2012-01-06 55 views
0

我不能得到的Facebook的Javascript功能的某些命令的jQuery裏面工作...使用Facebook「FB.Canvas.getPageInfo」裏面的jQuery功能

這工作:

$("a#GetScrollTest").click(function() { 
    FB.Canvas.getPageInfo(function(fbCanvasInfoObject) { 
     var fbPosition = fbCanvasInfoObject.scrollTop; 
     $("a#GetScrollTest").html("The scroll top value 
     is " + fbPosition + "px"); 
    }); 
    return false; 
}); 

這沒有按」 T(返回「未捕獲的ReferenceError:FB沒有定義」):

$("a.scrollLink").each(function() { 
    $(this).click(function(){ 
     FB.Canvas.getPageInfo(function(fbCanvasInfoObject) { 
     var fbPosition = fbCanvasInfoObject.scrollTop; 
     $(this).html("The scroll top value is " + fbPosition + "px"); 
     }); 
     return false; 
    }); 
}); 

誰知道爲什麼一個點擊方法可行,但在。每個()方法不?爲什麼它返回未定義的FB?電話在同一個地方。

回答

1

window.fbAsyncInit開始的那個大腳本塊的整個點是Facebook SDK以異步方式加載

儘管您在jQuery文檔就緒回調中針對FB調用了該函數,但這並不足以確保在該代碼執行時加載SDK。

幸運的是,window.fbAsyncInit對於確實存在那個目的:在SDK加載之前它不會運行。

Facebook's docs

The function assigned to window.fbAsyncInit is run as soon as the SDK is loaded. Any code that you want to run after the SDK is loaded should be placed within this function and after the call to FB.init. For example, this is where you would test the logged in status of the user or subscribe to any Facebook events in which your application is interested.

只動$("a.scrollLink").each(function()到fbAsynchInit功能,都應該是快樂的。

+0

我無法讓jquery在fbAsynchInit函數內運行。有什麼我需要添加從JavaScript切換到jquery裏面嗎?謝謝! – chuuke 2012-01-07 00:25:23

+0

我有我的網頁上的fbAsyncInit工作的jQuery。那裏沒有什麼特別的。 – DMCS 2012-01-07 00:42:43