2012-01-06 127 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