gpt4 book ai didi

javascript - Facebook 通过 FB.ui 分享

转载 作者:数据小太阳 更新时间:2023-10-29 05:15:35 25 4
gpt4 key购买 nike

我正在尝试使用我根据新文档编写的以下代码来显示“post to feed”对话框 (https://developers.facebook.com/docs/javascript/reference/FB.ui),但我收到以下错误“ReferenceError: FB is not defined

我使用的代码是我能想到的最简单的代码:

window.fbAsyncInit = function() {     
FB.init({
appId : 'xxxxxxxx',
status : true,
xfbml : true
});
};

(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 = "http://connect.facebook.net/en_US/all.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));


FB.ui({
method: 'feed',
name: 'Facebook Dialogs',
link: 'https://developers.facebook.com/docs/dialogs/',
picture: 'http://fbrell.com/f8.jpg',
caption: 'Reference Documentation',
description: 'Dialogs provide a simple, consistent interface for applications to interface with users.'
});

有什么想法吗?

编辑 1

如果我想在用户点击链接时打开对话框,我会使用 jquery click 事件

$(".userActions a.facebook").click(function() {
FB.ui({
method: 'feed',
name: 'Facebook Dialogs',
link: 'https://developers.facebook.com/docs/dialogs/',
picture: 'http://fbrell.com/f8.jpg',
caption: 'Reference Documentation',
description: 'Dialogs provide a simple, consistent interface for applications to interface with users.'
});
});

或者将 FB.ui 放在接受参数的函数中并调用此函数,例如

window.fbAsyncInit = function() {     
FB.init({
appId : 'xxxxxxxx',
status : true,
xfbml : true
});

// Code in here will run once FB has been initialised

function FB_post_feed(method,name,link,picture,caption,description){
FB.ui({
method: method,
name: name,
link: link,
picture: picture,
caption: caption,
description: description
});
}

};

(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 = "http://connect.facebook.net/en_US/all.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));

和 HTML 中的某处

$(".userActions a.facebook").click(function() {
FB_post_feed('feed','Facebook Dialogs','https://developers.facebook.com/docs/dialogs/','http://fbrell.com/f8.jpg','Reference Documentation','Dialogs provide a simple, consistent interface for applications to interface with users.')
}

最佳答案

好的,您在那里犯了一些概念性错误。

首先:

window.fbAsyncInit = function() {     
FB.init({
appId : 'xxxxxxxx',
status : true,
xfbml : true
});
};

(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 = "http://connect.facebook.net/en_US/all.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));

FB.ui({
method: 'feed',
name: 'Facebook Dialogs',
link: 'https://developers.facebook.com/docs/dialogs/',
picture: 'http://fbrell.com/f8.jpg',
caption: 'Reference Documentation',
description: 'Dialogs provide a simple, consistent interface for applications to interface with users.'
});

这是做什么用的?在没有用户交互的情况下打开共享对话框? FB.ui 必须在应该出现共享对话框时调用,而不是在 Facebook AsyncInit 函数之后立即调用。

此外,正如函数名称所示,facebook sdk 将异步启动。这意味着 FB 不会在你放置函数的地方定义。

第二:

window.fbAsyncInit = function() {     
FB.init({
appId : 'xxxxxxxx',
status : true,
xfbml : true
});

// Code in here will run once FB has been initialised

function FB_post_feed(method,name,link,picture,caption,description){
FB.ui({
method: method,
name: name,
link: link,
picture: picture,
caption: caption,
description: description
});
}
};

FB_post_feed 函数,在这种情况下,是 fbAsyncInit 函数内部的局部函数。因此,您无权访问 fbAsyncInit 之外的 FB_post_feed 函数。

此外,FB.init 是异步的,这意味着 FB 将在创建 FB_post_feed 时未定义。

取决于你的 HTML 代码是如何定义的,如果这个标识符是正确的,代码

$(".userActions a.facebook").click(function() {
FB.ui({
method: 'feed',
name: 'Facebook Dialogs',
link: 'https://developers.facebook.com/docs/dialogs/',
picture: 'http://fbrell.com/f8.jpg',
caption: 'Reference Documentation',
description: 'Dialogs provide a simple, consistent interface for applications to interface with users.'
});
});

应该可以正常工作。但是,只是一个建议:类通常用于设置样式。如果您改用按钮(或“a”元素)id,那将是最佳实践。

关于javascript - Facebook 通过 FB.ui 分享,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22377724/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com