Fb share button works only once without reloading the page - facebook

I'm trying to add several facebook share buttons on the page, but calling the "share" functions, that shares the current material on facebook, works only once at the page, after one click, you have to reload the page for another sharing.
Here is my HTML:
<button class="share js-share-btn">Share</button>
<button class="share js-share-btn">Share</button>
<button class="share js-share-btn">Share</button>
CSS:
body {
text-align: center;
}
.share {
display: inline-block;
width: 40px;
height: 20px;
margin: 20px;
}
And JS:
$(".js-share-btn").each(function(){
$(this).on('click', function(event){
event.preventDefault();
fbShare('title','description');
return false;
})
});
function fbShare(title, description, image) {
(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/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
window.fbAsyncInit = function() {
FB.init({
appId : 'id',
xfbml : true,
version : 'v2.8'
});
FB.ui({
method: 'share_open_graph',
action_type: 'og.shares',
action_properties: JSON.stringify({
object : {
'og:url': 'http://dla.dev',
'og:title': title,
'og:description': description,
'og:image': image
}
})
}, function(response){});
};
}
Here is link on codepen: http://codepen.io/jumkarto/pen/YprMbK
If anyone knows the solution of my problem, I would really appreciate you help.

Add this code in the very top of body :
<div id="fb-root"></div>
<script async defer crossorigin="anonymous" src="https://connect.facebook.net/cs_CZ/sdk.js#xfbml=1&version=v3.3"></script>
after that place Share Button :
<div class="fb-share-button" data-href="[your_page]" data-layout="button_count" data-size="large">
<a target="_blank"
href="https://www.facebook.com/sharer/sharer.php?u=[your_page];src=sdkpreparse"
class="fb-xfbml-parse-ignore">Sdílet
</a>
</div>
And just call function FB.XFBML.parse(); dynamicaly instead of reloading the page.
FB.XFBML.parse(); <-- fixed for me

Related

Auto refresh facebook comments causes flicker

We have a facebook comments plugin in our web code and we want to update the comments section in realtime.
For this we are using FB.XFBML.parse() routine to parse the FB DOM. However when do so we see a huge flicker as the fb-comments DOM is removed and added again.
Is there any way to can auto refresh the comments section without any flicker?
Here is our code
<!doctype html>
<html lang="en">
<head>
</head>
<body>
<div id="social" style="width:550px;height:100px">
<script>(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 = "https://connect.facebook.net/en_US/all.js#xfbml=1&version=v2.6"; fjs.parentNode.insertBefore(js, fjs); }(document, 'script', 'facebook-jssdk'))
</script>
<div id = parent>
<div id ="mycomments" class="fb-comments" data-href="http://www.example.com/#/home" data-order-by=reverse_time>
</div>
</div>
<script>
function removeElement(elementId) {
try{
// Removes an element from the document
var element = document.getElementById(elementId);
element.parentNode.removeChild(element);
} catch (e){
}
}
function addElement() {
try{
const div = document.createElement('div');
div.innerHTML = `<div id ="mycomments" class="fb-comments" data-href="http://www.example.com/#/home" data-order-by=reverse_time/>`;
document.getElementById('parent').appendChild(div);
} catch (e){
console.error(e);
}
}
setInterval(function(){
removeElement("mycomments");
addElement();
FB.XFBML.parse();
console.log("added again");
}, 5000)
</script>
</div>
</body>
</html>
Any pointers will be greatly helpful.
Thanks.

triggering share button with facebooks native button instead of own image html element

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>My Feed Dialog Page</title>
</head>
<body>
//I can trigger the FB.ui with this image here but thatś not what I want. I want to trigger the FB.ui with the test function with the native facebook-share button with the class fb-share-button below.
<img id="share_button" onclick="test();" src="">
<script>
window.fbAsyncInit = function() {
FB.init({
appId : '',
xfbml : true,
version : 'v2.2'
});
};
(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/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
function test(){
FB.ui({
method: 'share_open_graph',
action_type: 'og.likes',
action_properties: JSON.stringify({
object:'https://developers.facebook.com/docs/',
})
}, function(response){});
}
</script>
<div class="fb-share-button" data-href="http://testgenerall.webhomeschool.de/test_fb_ui.html" data-redirect="http://testgenerall.webhomeschool.de/test_fb_ui.html" data-layout="button_count"></div>
</body>
</html>
That´s not possible, you can only use the image of the share button, but you can´t use the share button plugin to trigger your own custom function.

Facebook like button in sencha architect

I have a problem with a facebook like button.
In the facebook developer, facebook give us 3 differents methods to integrate the like button in our app. In this example I use all the 3 methods.
I have created 3 buttons in sencha architect touch (Ext.Button). After I have created a basic function where I pass to the HTML buttons config the 3 methods:
var me = this,
htmlStrBase,
htmlStr,
htmlStrBase1,
htmlStr1,
htmlStrBase2,
htmlStr2;
htmlStrBase = '<div class="fb-like" data-href="https://www.facebook.com/tecnitalia?ref=ts&fref=ts" data-layout="button" data-action="like" data-show-faces="false" data-share="false"></div>';
htmlStrBase1 = '<iframe src="//www.facebook.com/plugins/like.php?href=https://www.facebook.com/tecnitalia?ref=ts&fref=ts&width&layout=button&action=like&show_faces=false&share=false&height=35&appId=838403862859130" scrolling="no" frameborder="0" style="border:none; overflow:hidden; height:35px;" allowTransparency="true"></iframe>';
htmlStrBase2 = '<html xmlns:fb="http://ogp.me/ns/fb#"><fb:like href="https://www.facebook.com/tecnitalia?ref=ts&fref=ts" layout="button" action="like" show_faces="false" share="false"></fb:like>';
me.getLikebutton().setHtml(htmlStrBase);
me.getLikebutton1().setHtml(htm
lStrBase1);
me.getLikebutton2().setHtml(htmlStrBase2);
naturally first I have launched the FB.Init (for the desktop and for the App):
if(Ext.os.is.Android || Ext.os.is.iOS) {
FB.init({
appId : 'xxxxxxxxxxxxx',
status : true,
nativeInterface: CDV.FB,
useCachedDialog: false,
xfbml : true
});
//alert('dopo FB.init');
FB.Event.subscribe('auth.statusChange', function(response) {
//alert('status changed');
});
(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/it_IT/sdk.js#xfbml=1&appId=XXXXXXXXX&version=v2.0";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
}
//Otherwise let's use the the web version
else
{
//Wait for sdk to load
//alert('eseguo fbAsyncInit');
window.fbAsyncInit = function() {
FB.init({
appId : 'XXXXXXXXXX',
status : true,
xfbml : true
});
FB.Event.subscribe('auth.statusChange', function(response) {
//alert('status changed');
});
};
//alert(' after fbAsyncInit e now include SDK');
//Include SDK
(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/en_US/all.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
}
the final result is that on the desktop, it functions perfectly but on my android App not yet...I Don't see any like button.
I say you last thing....if I copy one of this 3 methods directly in the html button config I can see the like button on my App. But I cannot copy directly this methods in the HTML config button because I need to change the facebook address each time a user logged in my App.
Somebody can help me and knows a method to integrate in my app the like button dinamically?
thank you
Carlo
Hi Guillard. Thank's. It's function but I have another problem. in android After the Like Login It show me a blank page!!!! No with mozilla or Firefox on desktop.
Thank you
Carlo
One thing that made a difference for me was adding the Facebook Scripts after my panel with fb-root id was already in place.
Here I created a Container with a bottom toolbar and a another container (fb-root) that has a Like button for the Sencha page.
Ext.create('Ext.Container', {
fullscreen: true,
html : 'My Sencha touch panel',
items: [
{
docked: 'top',
xtype: 'titlebar',
title: 'Sencha Touch',
},{
docked: 'bottom',
xtype: 'toolbar',
ui: 'light',
items: [{
xtype: 'container',
id: 'fb-root',
width: 50,
height: 20
}]
}
],
listeners: {
initialize : function (container) {
(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/en_US/sdk.js#xfbml=1&version=v2.0";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
var html = '<div class="fb-like" data-href="https://www.facebook.com/senchainc" data-layout="button" data-action="like" data-show-faces="false" data-share="false"></div>';
container.query('toolbar container')[0].setHtml(html);
}
}
});
Fiddle: https://fiddle.sencha.com/#fiddle/cmm

open graph and tpl file

i want to integrate open graph using sdk and facebook login button (token action)
the probleme is with the smarty template tpl file
in fact i tested all my code on simple html file, and it work like a charme : http://zdig1.biz/1.htm
the same code when i report it to tpl file don't work
of corse by adding {literal}
you can compare the other link with this
http://zdig1.biz/video/naruto-shippuden/317-video_7605be4c2.html
the facebook boutton called time line dont exit
and the publich one dont work
<div id="fb-root"></div>
{literal}<script type="text/javascript">
window.fbAsyncInit = function() {
FB.init({
appId : '48439698629',
channelUrl : '//zdig1.biz/channel.html',
status : true,
cookie : 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 = "//connect.facebook.net/en_US/all.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>{/literal}
{literal}<script type="text/javascript">
function postArticle()
{
FB.api(
'me/video.watches',
'post',
{
video: "http://zdig1.biz/video/naruto-shippuuden/321-video_f69d2a355.html"
},
function(response) {
if (!response || response.error) {
alert('Post was not published.');
} else {
alert('Post was published. Action ID: ' + response.id);
}
});
}
</script>{/literal}
and the action one
<fb:login-button onlogin = 'postArticle()' perms="email,publish_actions,user_actions.video">
Timeline
</fb:login-button>
or
<fb:add-to-timeline></fb:add-to-timeline>
<form>
<input type="button" value="Publish" onclick="postArticle()" />
</form>
the solution was :
<fb:login-button onlogin='postlike()' perms="email,publish_actions,user_actions.video">Timeline</fb:login-button>

why facebook FB.Event.subscribe is not working?

I have developed a web page as follow, would you please help me why this code does not work?
When i liked the page, alert in FB.Event.subscribe('edge.create' does not display!?
http://www.mandanemedia.com/freelancer/
<div >
<div id='subscribe' style='padding:10px; background:#fff;'>
<center>
<img src="fbbutton.jpg" style="width:290px; height:88px;"/>
</center>
<fb:like-box href="http://www.facebook.com/pages/MandaneMediacom/275554465891715" width="292" show_faces="true" border_color="#fff" stream="false" header="false"></fb:like-box>
</center>
</div>
</div>
<div id="fb-root"></div>
</body>
<script type="text/javascript">
window.fbAsyncInit = function() {
FB.init({
appId:'275554465891715',
status:true,
cookie: true,
xfbml: true
});
FB.Event.subscribe('edge.create', function(response) {
alert('You liked the URL: ' + response);
});
};
(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/en_US/all.js#xfbml=1";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>