Share button on my web application - javascript

I want to add a button to share a picture. This is my code in tag 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/it_IT/sdk.js#xfbml=1&version=v2.0";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
<div class="fb-share-button" data-href="my_image"></div>
Don't work. When i run my page.html it show an error:
GET file://connect.facebook.net/it_IT/sdk.js net::ERR_FILE_NOT_FOUND
However I WANT TO SHARE WITHOUT ID_APP!

Since you're testing this locally using a file path, "file:" is prepended onto the URL for the facebook script, hence the "file://connect.facebook.net". If you change the url to use "http:" or "https:" in the script, it should work as desired.

Related

Moving Facebook Like Button JavaScript to jQuery script file

I'm attempting to implement a Facebook like button on a web page on a domain which was recently upgraded to SSL. The sample code Facebook provides doesn't work for me. I keep getting this error when attempting to use Facebook's like button code:
Failed to execute 'postMessage' on 'DOMWindow': The target origin
provided (http://mywebsite.com) does not match the recipient window's
origin (https://mywebsite.com)
I completely cleaned out all http:// references throughout my project, so I'm a bit stumped. I did find a post which suggested the problem I'm encountering might have to do with the code running before the page is fully loaded. With that in mind, I'd like to delay running JavaScript until the page is loaded.
So let's say they want me to put this code just inside the <body> tag:
<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/sdk.js#xfbml=1&version=v2.10";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
Then I put my button somewhere on the page:
<div class="fb-like" data-href="https://www.facebook.com/MyPage/" data-width="100" data-layout="button" data-action="like" data-size="small" data-show-faces="false" data-share="false"></div>
How would I move the JavaScript they want me to embed inside the body of the <body> tag into my script.js file? I tried this, but I'm kinda rusty on jQuery, so I wanted to see if I'm fouling up what I'm attempting to do (spoiler: I probably am):
(function($) {
$(".fb-like").loaded(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.10";
// also tried adding https: in front of it, like so
// js.src = "https://connect.facebook.net/en_US/sdk.js#xfbml=1&version=v2.10";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'))
})
Update
Based on comments, I created an App on the Facebook developer portal, classified it as "Business" and enabled it publicly.
I put the following JavaScript code inside the <body> tag, per Facebook's instructions.
<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/sdk.js#xfbml=1&version=v2.10&appId=XXXXXXXXXXXXXXX";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
I also cleaned out the code I put in my scripts.js file so that all the code for the like button is on the html page.
I put the button inside a visible portion of the markup inside the <body> tag. I checked that the width of the button isn't set to zero. I refreshed the page, I nuked the browser cache (Safari and Chrome).
If I type https://mywebsite.com/my-page-with-like-button.html in the browser, I still get this error:
Failed to execute 'postMessage' on 'DOMWindow': The target origin
provided (http://mywebsite.com) does not match the recipient window's
origin (https://mywebsite.com)
...and nothing appears.
If I type in http://mywebsite.com/page-with-like-button.html in the browser, the page loads without an error, but the like button doesn't appear.
Any ideas re: what I'm mucking up are greatly appreciated. The last time I did this, it was a 5 minute project. Now, I'm on day 2 of fiddling with it.
To somewhat answer your question on how to add the FB script to an external js file. This is a best guess scenario; You could try and add it as it is in the example:
(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";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
I believe this would still need to be included before your <div> holding the like button. If you have other js inside that file, this should be at the top and outside of any other JS you have. for example:
(function(d,s,id){ /* FB script */}(document, 'script', 'facebook-jssdk'));
(function($){ /* your other JS */ })(jQuery);
This is purely a guess, and not the recommended 'FB official' way. So as long as you can add what you need in a js file just after the <body> element, that should work. I also assume you would need the correct markup for this in the HTML file(s) for this also, meaning:
<body>
<div id="fb-root"></div>
<script src="yourJsFileWithFB.js"></script>
....
.....
<div class="fb-like"></div>
...
.....
</body>
Plus all the data attributes referenced on the like button mentioned in the example HERE

Add share on facebook button on webpage

I am trying to add fb share in drupal site i want to do it with javascript so I followed following steps
Created on app page through Facebook developer
There I have created app and got appid and code to put on my template following is the code
<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#xfbml=1&version=v2.5&appId=myappid";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
I am getting:
Uncaught TypeError: Cannot read property 'fb_app_id' of undefined this error in
console
Do I need to link any other js file something I am missing because of that share button not coming

fb share button not displayed with ajax file

I used the following script in my web page
In body tag i used the following 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/sdk.js#xfbml=1&version=v2.0";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
In my blog listing I used the following in each blog:
<div class="fb-share-button" data-href="http://test.com" data-layout="button"></div>
Its displayed fine. After 10 blogs while scroll the remaining data are loaded dynamically by using the ajax file.
Here also I used the above script and Div share button. But it is not displayed with the ajax loaded file.
I googled a lot and I couldn't find a right solution. Please help me to fix this.
after ajax, call function FB.XFBML.parse(); to force the refresh/rendering FB share buttons

Like Button social plug in code not working for browser..!

Hi I am trying to run the same code on JSFIDDLE.NET and its working abs fine.
But When I run the code under body tag in browser it shows no result and error in chrome console:
In the end I want to same html file in webView on Android so that I can like a page.!
Failed to load resource file://connect.facebook.net/en_US/all.js#xfbml=1&appId=415120148602947
I think javascript is not running.
Code Goes like This:
<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=415120148602947";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
<div class="fb-like" data-href="http://developers.facebook.com/docs/reference/plugins/like" data-send="false" data-width="450" data-show-faces="true"></div>

facebook send button is not working with localhost

I have included the following code in my jsp page,
<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>
<div class="fb-send" data-href="http://www.172.28.188.com" data-font="tahoma"></div>
But it is not working,I think it is not accepting the data-href as "http://www.172.28.188.com",Please help me to find a solution.
There 2 things to consider here.
have u tried to remove the www from the url.
is the fbid and fbkey configured for the url u specified. https://developers.facebook.com/apps
check your configuration again

Categories