与此问题类似:FB.api only loads on first AJAX call to page
由于我的网站结构,页面是通过AJAX加载的。一切正常,除了第二次加载window.fbAsyncInit
函数的页面加载,它不起作用。控制台上没有显示任何错误,所有显示的都是我的auth-loggedout
div内容,其中包含facebook登录按钮,但点击它没有响应。
我确信我已登录,因为在上一页加载时我只是使用api发布到我的墙上。
有没有办法更改初始化,以便它可以在每个页面加载时起作用?
// Init the SDK upon load
window.fbAsyncInit = function() {
FB.init({
appId : '<?php echo $appID; ?>', // App ID
channelUrl : '//domain.co.uk/myoe/channel.html', // Path to your Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
// listen for and handle auth.statusChange events
FB.Event.subscribe('auth.statusChange', function(response) {
// lots of code
};
// 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 = "https://connect.facebook.net/en_US/all.js";
ref.parentNode.insertBefore(js, ref);
}(document));
答案 0 :(得分:1)
if (typeof FB !== 'undefined') {
// your code from window.fbAsyncInit = function() {
// but WITHOUT FB.init({...});
}
所以,如果你把公共代码放在一个函数中,你就可以从这两个函数调用这个函数,这样你就不会重复了。
对一个非常令人沮丧的问题做出了直截了当的回答。