And*_*das 6 javascript facebook-javascript-sdk fb.ui
我正在尝试使用我根据新文档编写的以下代码来显示"发布到源"对话框(https://developers.facebook.com/docs/javascript/reference/FB.ui)但我得到了以下错误" ReferenceError:FB未定义 "
我使用的代码是最简单的我可以提出:
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.'
});
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
编辑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.'
});
});
Run Code Online (Sandbox Code Playgroud)
或者在接受参数并调用此函数的函数内部使用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'));
Run Code Online (Sandbox Code Playgroud)
在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.')
}
Run Code Online (Sandbox Code Playgroud)
好的,你在那里做过一些概念上的错误.
第一:
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.'
});
Run Code Online (Sandbox Code Playgroud)
这该怎么办?无需用户交互即可打开共享对话框?当应该出现Share Dialog时,必须调用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
});
}
};
Run Code Online (Sandbox Code Playgroud)
在这种情况下,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.'
});
});
Run Code Online (Sandbox Code Playgroud)
应该工作正常.但是,只是一个建议:类通常用于设计一些东西.如果您使用按钮(或"a"元素)id,那将是最佳实践.
| 归档时间: |
|
| 查看次数: |
31230 次 |
| 最近记录: |