Facebook javascript SDK under Codeigniter and JqueryMobile - facebook

My website is using both Codeigniter and JqueryMobile. I am trying to insert a facebook share button in my website.
The button only show at the first time. However, It will be disappeared if you go to other page and back to the original one.
I discover that the below code run well under either Codeigniter or JqueryMobile. But the problem occurs if both framework are used at the same time.
<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 = "//connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
<div class="fb-share-button" data-href="https://developers.facebook.com/docs/plugins/" data-layout="button"></div>

Related

Facebook customer chat plugin implementation with Tag Manager error

Working solution
Found the answer on this article by Simoa Hava
Problem
We have implemented the Facebook customer chat using GTM. While version 2.11 worked fine, v2.12 doesn't work as it doesn't allow me to add the code below
Implementation guidelines
<!-- Load Facebook SDK for JavaScript -->
<div id="fb-root"></div>
<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_GB/sdk.js#xfbml=1&version=v2.12&autoLogAppEvents=1';
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
<!-- Your customer chat code -->
<div class="fb-customerchat"
page_id="xxxxxxxxxxxxxx"
theme_color="#921710"
logged_in_greeting="Hi! Got any questions? Ask us anything, or get stuck in for more inspiration!"
logged_out_greeting="Hi! Got any questions? Ask us anything, or get stuck in for more inspiration!">
</div>
The error I get is
Invalid HTML, CSS or JavaScript found in template.
Previous working code:
<script>
window.fbAsyncInit = function() {
FB.init({
appId : 'xxxxxxxxxxxxxx',
autoLogAppEvents : true,
xfbml : true,
version : 'v2.11'
});
};
(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/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
jQuery('body').append('<div class="fb-customerchat" page_id="xxxxxxxxxx" minimized="false"> </div>');
</script>
Updated code with jQuery - Still shows error
<!-- Load Facebook SDK for JavaScript -->
<div id="fb-root"></div>
<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_GB/sdk.js#xfbml=1&version=v2.12&autoLogAppEvents=1';
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
<!-- Your customer chat code -->
jQuery('body').append('<div class="fb-customerchat" page_id="xxxxxxxxxxx" theme_color="#921710" logged_in_greeting="Hi! Got any questions? Ask us anything, or get stuck in for more inspiration!" logged_out_greeting="Hi! Got any questions? Ask us anything, or get stuck in for more inspiration!" minimized="true"></div>');
Any help will be much appreciated :)

My facebook like button is not showing up

I looked at previous asked questions about the facebook like button and didnt see anything that was wrong with my code. Here is what I have:
<body>
<div id="fb-root"></div>
<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 = "//connect.facebook.net/en_US/all.js#xfbml=1";
fjs.parentNode.insertBefore(js, fjs);}
(document, 'script', 'facebook-jssdk'));</script>
</body>
After adding the code for initializing the Facebook's Javascript SDK you also have to add the code to display the Like button at the required place. The generic code for it is
<div class="fb-like" data-send="true" data-width="450" data-show-faces="true">
</div>
If you want you can further customize it by checking documentation here.
Had the same problem, once I published the page, the like buttons showed up
facebook page : settings : Page Visibility : Page published
(the page was "unpublished" by default even after I did publish it for the first time)
I know topic is old but still coming up in searches.
Here's the initialization code I used:
(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/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
Important:
By default Facebook auto-configuration tool (https://developers.facebook.com/docs/plugins/follow-button) does not prepend "HTTPS" when exporting the js.src element.
If testing this on localhost host you will run into problems with the button visibility (c.f. v2.9) Either use a https tunnel service such as ngrok or deploy to valid https server.
Adding version to the url js.src = "https://connect.facebook.net/en_US/sdk.js#xfbml=1&version=v2.11"; helped in my case.

Why is the facebook script include overcomplicated?

The documentation for the Facebook like button suggests including the following script:
<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 = "//connect.facebook.net/en_GB/all.js#xfbml=1";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
As I read it, this does nothing more than add the external all.js file before the first script tag on the page.
<script id="facebook-jssdk" src="//connect.facebook.net/nl_NL/all.js#xfbml=1"></script>
Is there any reason to not just add the completed script tag to the page source in the first place?
From the Javascript SDK page;
This code loads the SDK asynchronously so it does not block loading other elements of your page.
In other words, yes, you could most likely include it right away, but the user would experience it as if your page were loading more slowly.
What is the difference between this:
<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 = "//connect.facebook.net/en_GB/all.js#xfbml=1";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
and this:
<script src="//connect.facebook.net/nl_NL/all.js#xfbml=1" async></script>
?

Facebook comment plugin code

I've seen some post up already around the same subject but not really what I wanted to know
But i'm trying to add the facebook comment box plug in, into my site but it doesn't seem to be working, this is the code it has given me
<div id="fb-root"></div>
<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 = "//connect.facebook.net/en_GB/all.js#xfbml=1&appId=***************";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
<div class="fb-comments" data-href="http://www.facebook.com/pages/Cuba-Street/86653690342?fref=ts" data-num-posts="2" data-width="470"></div>
But it just doesn't seem to return anything?
I checked your code without single modification, it works for me

Adding "via MyApp" text to Facebook Social Plugins

With Facebook API I can create a simple "Post to my Wall" app, which adds a "via MyApp" text to every post that is published by user.
However, if a user adds a comment using Comments Box their post will show on their Wall without the "via MyApp" text. The same situation takes place when a user clicks on the Like Button and leaves a comment to their like.
What can be done to include the "via MyApp" text?
My code:
<div id="fb-root"></div>
<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 = "//connect.facebook.net/en_US/all.js#xfbml=1&appId=MYAPPID";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
<fb:comments href="http://example.com" num_posts="2" width="470"></fb:comments>
In your comment box script like the one below just replace appId=3453534536334 with your application ID of MyApp
<div id="fb-root"></div>
<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 = "//connect.facebook.net/en_US/all.js#xfbml=1&appId=3453534536334";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>