I am trying to use the following code that I wrote based on the new documentation for showing a "post to feed" dialog (.ui) but i get the following error "ReferenceError: FB is not defined"
the code that i use is the simplest i can e up with:
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 = ".js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
FB.ui({
method: 'feed',
name: 'Facebook Dialogs',
link: '/',
picture: '.jpg',
caption: 'Reference Documentation',
description: 'Dialogs provide a simple, consistent interface for applications to interface with users.'
});
Any ideas?
EDIT 1
and if i want to open the dialog when a user clicks on a link i would use jquery click event
$(".userActions a.facebook").click(function() {
FB.ui({
method: 'feed',
name: 'Facebook Dialogs',
link: '/',
picture: '.jpg',
caption: 'Reference Documentation',
description: 'Dialogs provide a simple, consistent interface for applications to interface with users.'
});
});
or have the FB.ui inside a function that accepts parameters and call this function e.g.
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 = ".js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
and somewhere in the HTML
$(".userActions a.facebook").click(function() {
FB_post_feed('feed','Facebook Dialogs','/','.jpg','Reference Documentation','Dialogs provide a simple, consistent interface for applications to interface with users.')
}
I am trying to use the following code that I wrote based on the new documentation for showing a "post to feed" dialog (https://developers.facebook./docs/javascript/reference/FB.ui) but i get the following error "ReferenceError: FB is not defined"
the code that i use is the simplest i can e up with:
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/en_US/all.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
FB.ui({
method: 'feed',
name: 'Facebook Dialogs',
link: 'https://developers.facebook./docs/dialogs/',
picture: 'http://fbrell./f8.jpg',
caption: 'Reference Documentation',
description: 'Dialogs provide a simple, consistent interface for applications to interface with users.'
});
Any ideas?
EDIT 1
and if i want to open the dialog when a user clicks on a link i would use jquery click event
$(".userActions a.facebook").click(function() {
FB.ui({
method: 'feed',
name: 'Facebook Dialogs',
link: 'https://developers.facebook./docs/dialogs/',
picture: 'http://fbrell./f8.jpg',
caption: 'Reference Documentation',
description: 'Dialogs provide a simple, consistent interface for applications to interface with users.'
});
});
or have the FB.ui inside a function that accepts parameters and call this function e.g.
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/en_US/all.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
and somewhere in the HTML
$(".userActions a.facebook").click(function() {
FB_post_feed('feed','Facebook Dialogs','https://developers.facebook./docs/dialogs/','http://fbrell./f8.jpg','Reference Documentation','Dialogs provide a simple, consistent interface for applications to interface with users.')
}
Share
Improve this question
edited Mar 13, 2014 at 12:13
Andreas Traganidas
asked Mar 13, 2014 at 11:50
Andreas TraganidasAndreas Traganidas
5131 gold badge7 silver badges17 bronze badges
0
3 Answers
Reset to default 7Ok, there are a few conceptual mistakes you've done there.
First:
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/en_US/all.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
FB.ui({
method: 'feed',
name: 'Facebook Dialogs',
link: 'https://developers.facebook./docs/dialogs/',
picture: 'http://fbrell./f8.jpg',
caption: 'Reference Documentation',
description: 'Dialogs provide a simple, consistent interface for applications to interface with users.'
});
What is this supposed to do? Open a share dialog without user interaction? FB.ui must be called when Share Dialog is supposed to appear, not right after Facebook AsyncInit function.
And, also, facebook sdk will be started Asynchronously, as the name of the function suggests. It means FB will not be defined in the place you put the function.
Second:
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 function, in this case, is a local function inside fbAsyncInit function. So, you don't have access to FB_post_feed function outside fbAsyncInit.
Besides, FB.init is asynchronous, which means FB will be undefined by the time FB_post_feed is being created.
Depending on how your HTML code is defined, and if this identifier is correct, the code
$(".userActions a.facebook").click(function() {
FB.ui({
method: 'feed',
name: 'Facebook Dialogs',
link: 'https://developers.facebook./docs/dialogs/',
picture: 'http://fbrell./f8.jpg',
caption: 'Reference Documentation',
description: 'Dialogs provide a simple, consistent interface for applications to interface with users.'
});
});
should work fine. However, just an advice: Classes are usually used to style something. It would be best-practice if you use the button(or "a" element) id instead.
I had the same problem and solved it by requesting the Facebook script by jQuery and then initializing it:
function FB_post_feed(method,name,link,picture,caption,description){
FB.ui({
method: method,
name: name,
link: link,
picture: picture,
caption: caption,
description: description
});
}
$(document).ready(function()
{
$.getScript("http://connect.facebook/en_US/all.js#xfbml=1", function () {
FB.init({ appId: 'xxxxxxxx', status: true, cookie: true, xfbml: true });
});
});
Add this in your index.html
<script type="text/javascript" src="https://connect.facebook/en_US/sdk.js"></script>