Facebook XFBML 和 Internet Explorer 8 异步加载问题
我正在尝试在页面上实现 XFBML 评论框。它可以在 Firefox 和 Chrome 中运行,但只能在 Internet Explorer 8 中偶尔运行。
当页面点击 FB.XFBML.parse('fb-stuff');
时,我收到“FB 未定义”错误。在尝试解析 XFBML 之前,是否需要检查 Facebook Connect 脚本是否已完成加载?
<script type="text/javascript">
window.fbAsyncInit = function() {
FB.init({
appId : '117378991625799',
status : false, // check login status
cookie : false, // 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);
FB.XFBML.parse('fb-stuff');
}());
</script>
I'm trying to implement an XFBML comment box on a page. It works in Firefox and Chrome, but only sporadically in Internet Explorer 8.
I get an 'FB is undefined' error when the page hits the FB.XFBML.parse('fb-stuff');
. Do I need to check if the Facebook Connect script has finished loading before I try to parse the XFBML?
<script type="text/javascript">
window.fbAsyncInit = function() {
FB.init({
appId : '117378991625799',
status : false, // check login status
cookie : false, // 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);
FB.XFBML.parse('fb-stuff');
}());
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不需要自己调用 fb.xfbml.parse,因为您已经指示 facebook init 在 fbAsyncInit 中为您执行此操作。它主要在初始化后向页面添加新的 facebook 元素并希望解析和呈现它们的情况下有用。
是的,您应该等待 javascript 库加载。这就是 fbAsyncInit 的全部内容。您为 connect 库创建脚本元素并将其注入到该匿名函数中的 dom 中,但浏览器需要一些时间来加载和评估代码,因此 FB 无法立即使用。当脚本加载后(并且 FB 对象可用),它将尝试执行 window.fbAsyncInit,然后您可以执行任何您需要的操作。
You shouldn't need to call fb.xfbml.parse yourself, since you're already instructing facebook init to do that for you in the fbAsyncInit. It's useful mainly in situations when you add new facebook elements to the page after the initialization and want them to be parsed and rendered.
And yes, you should wait for the javascript library to load. That's what the fbAsyncInit is all about. You create the script element for the connect library and inject it into the dom in that anonymous function, but it takes a little while for the browser to load and evaluate the code, so FB isn't available straight away. When the script has loaded (and the FB object is available) it will try to execute the window.fbAsyncInit and then you can do whatever you need to.