Why is the facebook script include overcomplicated? - facebook

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>
?

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 :)

Facebook javascript SDK under Codeigniter and JqueryMobile

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>

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

Facebook "Like it" button localization using Microsoft.Web.Helpers

I've implemented "Like" button on my page using Microsoft.Web.Helpers. It is however in English. How can I change it to be Polish for instance?
You need to change the locale. There is a locale in the path in the Javascript SDK's URL.
Most examples use en_US for the locale:
<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={appid}";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
So you will need to replace that with your locale.
For more information please read: https://developers.facebook.com/docs/reference/plugins/like/

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>