有时我在加载 http://connect.facebook.net/en_US/all.js 时遇到“FB 未定义”问题
我意识到问题是因为有时我的网站无法加载该文件。所以它什么也得不到,对象 FB 实际上不存在。
我的解决方案是在发生这种情况时阻止我的用户,所以我在 JavaScript 中尝试了以下代码,但似乎都不起作用:
if (FB) {/*run the app*/} else {/*alert the user*/}
if (FB!==false) {/*run the app*/} else {/*alert the user*/}
if (FB!='undefined') {/*run the app*/} else {/*alert the user*/}
感谢您的回答!
最佳答案
我认为你应该解决主要问题,Facebook 提供了哪个解决方案(异步加载 SDK):
You should insert it directly after the opening tag on each page you want to load it:
<script>
window.fbAsyncInit = function() {
FB.init({
appId : 'your-app-id',
xfbml : true,
version : 'v2.1'
});
};
(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
来自documentation :
The Facebook SDK for JavaScript doesn't have any standalone files that need to be downloaded or installed, instead you simply need to include a short piece of regular JavaScript in your HTML that will asynchronously load the SDK into your pages. The async load means that it does not block loading other elements of your page.
更新:使用文档中的最新代码。
关于javascript - 如何解决 'FB is not defined' ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5331165/