我在我的网站上使用facebook javaSDK,但我遇到了一个奇怪的问题。
当我使用“ Internet Explorer”加载网站时,它总是可以正常运行。
当我用firefox加载网站时,它总是加载TWICE,而当我使用chrome时,JSDK有时加载一次,有时根本不加载,给我错误消息“未捕获的ReferenceError:未定义FB”。
我尝试对SDK使用异步加载,但仍然存在相同的问题。
顺便说一句,我在firefox上安装了萤火虫,这可能就是JSDK加载两次的原因吗?
这是用于加载jssdk的代码
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : '108437552626598', // App ID
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
};
// Load the SDK Asynchronously
(function(d){
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
ref.parentNode.insertBefore(js, ref);
}(document));
</script>
我试图使用FB.getlogin,我试图在Wordpress网站中整合facebook opengraph。完成主要内容的加载后,立即退出脚本
这是代码
</div><!-- end #content -->
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
FB.getLoginStatus(function(response) {
if (response.status == 'connected') {
alert('in');
window.facebooklogin = 'true';
window.myValue = response.authResponse.accessToken;
window.myId = response.authResponse.userID;
$('.userimage img').attr('src','http://graph.facebook.com/' + window.myId + '/picture');
$.ajax({ url: 'http://ballvids.com/check/admin_checkuser.php',
data: {action: window.myId},
dataType:'json',
type: 'post',
success: function(output) {
if (output.message == 'true'){
$('.thumbnail img').attr('src',"http://ballvids.com/images/on2.png");
alert(window.myValue);
FB.api('/me/video.watches?video=<?php echo urlencode(get_permalink($post->ID));?>&access_token='+window.myValue,'post',function(response) {
if (!response || response.error) {
alert(response.error.message);
} else {
alert(response.id);
postID=response.id;
showStuff('removeTimeline');
}
});
}else{
$('.thumbnail img').attr('src',"http://ballvids.com/images/off2.png");
}
}
});
}else{
alert('inside2');
$('.thumbnail img').attr('src',"http://ballvids.com/images/off2.png");
window.facebooklogin = 'false';
}
},真正);
谢谢
最佳答案
您必须在FBLoginStatus()
内调用fbAsyncInit
window.fbAsyncInit = function() {
FB.init({
appId : APP_ID,
status : true,
cookie : true,
xfbml : true
});
FB.getLoginStatus( function(response) {
console.log(response);
}, true);
};
如果您对从
FB.getLoginStatus()
返回的缓存对象有疑问,则必须传递第二个参数true
。文档:https://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus/