我试图发布到 facebook 新闻源,但弹出框不时出现。我认为这与回发或其他事情有关。这是发生了什么:用户再见并在弹出窗口中发送到 Payson(payson 是瑞典版的 Paypal),然后 payson 将用户发送回我的页面,仍然在弹出窗口中。这是一个运行的代码:
function postToFeed() {
try {
FB.getLoginStatus(function (response) {
if (response.status === 'connected') {
var obj = {
method: 'feed',
link: 'link',
picture: 'imagelink',
name: 'title',
caption: 'some text...',
description: 'some text...',
redirect_uri: 'url'
};
function callback(response) {
document.getElementById('msg').innerHTML = "Post ID: " + response['post_id'];
}
FB.ui(obj, callback);
}
else {
alert("not connected")
}
});
} catch (e) {
alert(e.Message)
}
}
现在发生的事情是我从我的 try catch 中得到了错误,它只是说FB is not defined。
如果我之后使用按钮运行此功能,它就可以正常工作。如果我从 payson 伪造回发,它也可以工作,但如果我在新窗口中进行,则不会。
我试图将 FB.init 函数放入我的函数中,但这没有帮助。
谢谢你的帮助!
新代码
这是在一个 .js 文件中
var isLoaded = false;
window.fbAsyncInit = function () {
FB.init({
appId: '00000000', // App ID
channelUrl: 'url', // Channel File
status: true, // check login status
cookie: true, // enable cookies to allow the server to access the session
xfbml: true, // parse XFBML
oauth: true // enable OAuth
});
isLoaded = true;
};
// 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));
(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/sv_SE/all.js#xfbml=1&appId=503537396325197";
fjs.parentNode.insertBefore(js, fjs);
} (document, 'script', 'facebook-jssdk'));
FB.init({ appId: "000", status: true, cookie: true });
function postToFeed() {
var obj = {
display: 'iframe',
method: 'feed',
link: 'url',
picture: 'url',
name: 'text',
caption: 'text',
description: 'text',
redirect_uri: 'url'
};
function callback(response) {
document.getElementById('msg').innerHTML = "Post ID: " + response['post_id'];
}
FB.ui(obj, callback);
}
function init() {
FB.init({
appId: '00000',
status: true, // check login status
cookie: true, // enable cookies to allow the server to access the session
xfbml: true, // parse XFBML
channelUrl: 'url', // custom channel
oauth: true // enable OAuth
});
FB.getLoginStatus(function (response) {
if (response.status == 'connected') {
var user_id = response.authResponse.userID;
var access_token = response.authResponse.accessToken
var page_id = "00000"; //page id
var fql_query = "SELECT uid FROM page_fan WHERE page_id =" + page_id + " and uid=" + user_id;
var the_query = FB.Data.query(fql_query);
the_query.wait(function (rows) {
if (rows.length == 1 && rows[0].uid == user_id) {
$("#container").show();
$("#container_notlike").hide();
$("#notloggedin").hide();
}
else {
$("#container_notlike").show();
$("#container").hide();
$("#notloggedin").hide();
login();
}
});
} else {
$("#container_notlike").hide();
$("#container").hide();
login();
}
});
}
init();
// whenever the user logs in, we refresh the page
FB.Event.subscribe('auth.login', function (response) {
top.location.href = "url";
});
FB.Event.subscribe('auth.logout', function (response) {
top.location.href = "url";
});
function login() {
FB.login(function (response) {
if (response.authResponse) {
console.log('Logged in');
} else {
console.log('User cancelled login or did not fully authorize.');
}
}, { scope: 'email,user_likes,publish_actions' });
}
function likereload() {
FB.Event.subscribe('edge.create', function (response) {
top.location.href = "url"
});
}
function getuserinfo() {
FB.getLoginStatus(function (response) {
if (response.authResponse) {
FB.api('/me', function (response) {
document.getElementById("txtfName").value = response.first_name;
document.getElementById("txtlName").value = response.last_name;
document.getElementById("txtemail").value = response.email;
});
}
});
}