类似这个问题: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 : '', // 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 = "../../../en_US/all.js";
ref.parentNode.insertBefore(js, ref);
}(document));
好的,最终解决了:
if (typeof FB !== 'undefined') {
// your code from window.fbAsyncInit = function() {
// but WITHOUT FB.init({...});
}
因此,如果您将通用代码放入一个函数中,您就可以从两个函数中调用该函数,这样您就不会重复自己。
对一个真正令人沮丧的问题的相当简单的答案。
Tôi là một lập trình viên xuất sắc, rất giỏi!