即使我刚刚使用它,FB是未定义的 [英] FB is undefined even though I just used it
问题描述
我正在尝试添加一个Facebook Like按钮到我正在创建的小部件。我用来将Facebook添加到我的页面的代码如下:
widget.html
<身体GT;
< div id =fb-root>< / div>
< script>
window.fbAsyncInit = function(){
FB.init({
appId:'263071593731910',
status:false,//检查登录状态
cookie: true,//启用Cookie以允许服务器访问会话
xfbml:true // parse XFBML
});
};
(function(){
var e = document.createElement('script');
e.src = document.location.protocol +'//connect.facebook.net/en_US/ all.js';
e.async = true;
document.getElementById('fb-root')。appendChild(e);
}());
< / script>
< div id =fb-button>< / div>
< script type =text / javascriptsrc =widget.js>< / script>
widget.js
$(function(){
var fb = $(document.createElement(fb:like));
fb.attr({
href:data。 Facebook,
发送:false,
布局:'button_count',
width:70,
'show_faces':false
});
$( #fb-button)。empty()。append(fb);
FB.XFBML.parse($(#fb-button)。get(0));
FB.Event .subscribe('edge.create',changeView);
});
*在JavaScript中也存在changeView函数。
当我运行代码时,我收到一个错误:未捕获参考错误:FB未定义
即使按钮被创建。错误指向包含FB.XFBML.parse代码的行。在我的JavaScript中有什么需要做的不同吗?
从 window.fbAsyncInit
是Facebook SDK被异步加载。
即使你已经您在jQuery文档中调用 FB
可以回调,这不足以确保在执行代码时加载SDK。
幸运的是, / p>
从 Facebook的文档:
只要SDK
加载,分配给window.fbAsyncInit
的功能就会运行。在SDK加载
之后要运行的任何代码应放在此函数中,并在调用FB.init之后。
例如,您可以在此测试
用户的登录状态,或订阅您的应用程序是
感兴趣的任何Facebook事件。
I am trying to add a Facebook Like button to a widget that I am creating. The code that I use to add the Facebook like button to my page is as follows:
widget.html
<body>
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : '263071593731910',
status : false, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
};
(function() {
var e = document.createElement('script');
e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
e.async = true;
document.getElementById('fb-root').appendChild(e);
}());
</script>
<div id="fb-button"></div>
<script type="text/javascript" src="widget.js"></script>
widget.js
$(function(){
var fb = $(document.createElement("fb:like"));
fb.attr({
href: data.facebook,
send: false,
layout: 'button_count',
width: 70,
'show_faces': false
});
$("#fb-button").empty().append(fb);
FB.XFBML.parse($("#fb-button").get(0));
FB.Event.subscribe('edge.create',changeView);
});
*The changeView function does exist as well in the JavaScript.
When I run the code, I get an error: Uncaught ReferenceError: FB is not defined
even though the button is created. The error is pointing to the line containing FB.XFBML.parse code. Is there something I need to do differently in my JavaScript?
The whole point of that big script block starting with window.fbAsyncInit
is that the Facebook SDK gets loaded asynchronously.
Even though you've got your calls against FB
inside a jQuery document ready callback, that isn't sufficient to ensure the SDK is loaded when that code is executed.
Fortunately, window.fbAsyncInit
exists for exactly that purpose: it won't be run until the SDK has loaded.
From 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.
这篇关于即使我刚刚使用它,FB是未定义的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!