Share in facebook with out refreshing the page - facebook

I want to use facebook javascript sdk to share a page on facebook, and when a user share successfully I want to be able to track that on the current page without refreshing current page.
Now I have this code bellow, that opens the dialog successfully and share as well, but the popup doesn't close when I share or cancel. it only close when I use the cross in the top right corner.
Second problem is, I don't get notified when the user share successfully.
Note : if I use the code that fires the share dialog in $document.ready() instead of the click event on the open link, then it closes correctly, and call the attached functions as well where I console.log(response); but it has the same results for both successful share or cancel.
If I use feed dialog instead, the problem with closing the dialog stays, however the notifications is fine, I get notified back with the post_id, and that is perfect but I want to use share dialog not feed.
Bellow is all the code, sorry I couldn't make a jsfiddle, I tried but couldn't get facebook app work there.
Open
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<div id="fb-root"></div>
<script>
$(document).ready(function() {
$.ajaxSetup({ cache: true });
$.getScript('//connect.facebook.net/en_US/sdk.js', function(){
FB.init({
appId: '905779996145989',
version: 'v2.3'
});
});
$('.share').click(function() {
/*FB.ui({
method: 'feed',
link: 'https://developers.facebook.com/docs/',
caption: 'An example caption'
}, function(response){
console.log(response);
});*/
FB.ui({
method: 'share',
href: 'https://developers.facebook.com/docs/',
}, function(response){
console.log(response);
});
})
});
</script>

Related

Facebook share dialog does not close after posting or cancelling

Using v2.0 of the Facebook API, the share dialog popup doesn't close after the "Cancel" or "Post to Facebook" button has been selected. Previously, a 'redirect_uri' param was passed to the Feed endpoint, and this could be used to close the window. When trying to pass this param to the Share endpoint, however, I get the message: 'When using FB.ui, you should not specify a redirect_uri.'
Is there any way to force the popup window to close? And if not, might there be a problem with the API request? The link DOES get shared when "Post to Facebook" is selected, so I don't think it's an issue with login or permissions.
Here is what the API call looks like:
FB.ui({
method: 'share',
href: location.href,
)}, function(response){});
Try adding a preventDefault. I had the FB.ui action being triggered when an anchor tag was clicked. When I clicked the link, the page refreshed. Then when I'd click 'Post to Facebook' in the popup, I presume that the callback had nowhere to go.
What worked for me:
handleFbShare = (e) => {
e.preventDefault();
FB.ui({
method: 'share',
display: 'popup',
href: 'https://your-url/',
}, function(response){});
}
(...)
<a href="" className='button__facebook' onClick={this.handleFbShare}>Share on Facebook</a>
Hope that helps!

Facebook Share API - options for "link"

I'm struggling to create my first Facebook share feature on a website and the issue is a dynamically created link. I'm trying to share a referal link to (i.e. come checkout ecommerce site and get $20 back).
<script type="text/javascript">
$(document).ready(function(){
$('#share_button').click(function(e){
e.preventDefault();
FB.ui(
{
method: 'feed',
name: 'Want $20 off Sailormade bracelets for summer? Shop now and thank me later',
link: '<?php echo $this->getReferrerLink();?>',
picture:'http://shop.sailormadeusa.com/media/wysiwyg/raf/refer_a_friend_static_block.jpg'
});
});
});
First timer on the Facebook app, and first timer posting here on stackoverflow. Go easy on me, and let me know if I can provide any additional information. THanks!

Make link in Send Dialog to Facebook application?

Hi so I am making a Facebook app which uses the send dialog. I was wondering why it doesn't work when I put 'https://apps.facebook.com/APP_NAME/' as my link parameter. I want the link in the send message to be a link to my app but on the send dialog is appears as apps.facebook.com. Is there anyway to do this?
Edit: Yes sorry here is the code:
<script>
FB.init({appId: 'App_ID', xfbml: true, cookie: true});
function sendMessage(){
// assume we are already logged in
var token = FB.getAuthResponse().accessToken;
FB.ui({
method: 'send',
name: 'Test',
link: 'https://www.apps.facebook.com/APP_NAME',
description: 'This is a test',
picture: '',
display: 'iframe',
access_token: token
});
}
</script>
If you dont want the apps.facebook.com domain showing up in the caption for the post, you ca theren create a new page in your site. Set all the og meta tags to what you want to show up in the feed. The only other content in the page should just be a javascript redirect to your you canvas app. This way when FB scrapes your site, they get all the necessary data and when a user clicks on the link, the js redirects them to your app.
Also, there is not a "www" in the url for app canvas pages.

Facebook Login button does not trigger page refresh, FB.Event.subscribe doesn't seem to be registering

I have Login with Facebook on my site. I'm using the JS-SDK, coupled with the PHP-SDK.
I'm 99.9% sure I have all the correct files in the correct places, as the actual login works. However, I cannot get the page to fire a reload once the user has been logged in.
This is the facebook login button code ('Scope' is the new 'perms', as I just found out today):
<fb:login-button scope="email,user_birthday">Sign In</fb:login-button>
Once the user authorizes, etc, the Dialog closes, but then nothing happens. The user isn't logged in yet, and the page does not reload.
When I manually reload, the user is logged in.
This is the code I have in my footer that should listen for the login:
<script>
FB.Event.subscribe('auth.login', function(response) {
window.location.reload();
});
</script>
In my Chrome developer tools JS console, I get an error saying "Uncaught Reference: FB is not defined". This error is there as soon as the page loads, even before the Facebook Login button is clicked.
Any ideas as to what I might be doing wrong? Thanks!
I've found that passing the application secret to the auth.login call causes the event to fire correctly. You can pass the application secret from PHP to the javascript function, like this:
<script>
FB.Event.subscribe('auth.login', function(response) {
window.location.reload();
}, secret: '<?php print $facebook->getApiSecret(); ?>');
</script>
This has since been fixed by Facebook.

Facebook JavaScript SDK: FB.ui opens a popup window

I am trying to show a 'Post to Your Wall' feed dialog with the following code in a facebook iframe app:
<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js">
</script>
<script>
FB.init({
appId:'249725835046216', cookie:true,
status:true, xfbml:true
});
FB.ui({ method: 'feed',
message: 'Facebook for Websites is super-cool',
name: 'Facebook Dialogs',
link: 'http://developers.facebook.com/docs/reference/dialogs/',
picture: 'http://fbrell.com/f8.jpg',
caption: 'Reference Documentation',
description: 'Dialogs provide a simple, consistent interface for applications to interface with users.'
});
</script>
The problem is that it is appearing in a new popup window like this:
Instead of appearing as like this without appearing in a popup window:
I don't want the feed dialog to appear in a new popup windows because in most modern web browsers where popups are blocked. I don't know why this is happening. Please help.
I'm pretty sure that you get a popup if the user has not authorized your application. Facebook made it work that way for security reasons. If you prompt for authorization first, then you should get the inline dialog.
Note that the request for authorization will itself be a popup, but you only have to have that happen once. I have things working this way, the way you want, in the someecards Facebook app. Feel free to grab the javascript code, it's not specific to the app.
Try this, put FB.ui inside FB.getLoginStatus:
FB.getLoginStatus(function(response) {
if (response.authResponse) {
FB.ui({
method: 'feed',
...
display: 'dialog'
});
}
});
in my case the problem seems to have been solved by seting display to async
display: 'async',
i think this is default for page tabs and canvas, but from time to time, instead of appearing within the main window it would load a new popup..
after setting it though (page tab in my case) i haven't noticed any pop up coming up since then..
I know this is a bit old, but I stumbled across this page when trying to solve this problem for myself and none of the answers here worked for me.
For the benefit of anyone else who has this problem, this was happening for me because I was trying to call the dialog on page load. Moving it to a user triggered event (such as a click) resolved it for me.
I have the same UI issue and I don't like the pop up window too.
I just found a link.
It helps us to redirect the page in same window. But it does not solve our problem perfectly.
There's a "URL Redirection" section in the docs for the Feed Dialog:
https://www.facebook.com/dialog/feed?
app_id=145634995501895
&display=popup&caption=An%20example%20caption
&link=https%3A%2F%2Fdevelopers.facebook.com%2Fdocs%2F
&redirect_uri=https://developers.facebook.com/tools/explorer
So you can do window.location=(this url) in Javascript, setting the redirect_url correctly, and this should work without a popup.
Note that the Feed Dialog is now deprecated in v2.0, so check out the Share Dialog instead:
To share a link:
https://www.facebook.com/dialog/share?
app_id=145634995501895
&display=popup
&href=https%3A%2F%2Fdevelopers.facebook.com%2Fdocs%2F
&redirect_uri=https%3A%2F%2Fdevelopers.facebook.com%2Ftools%2Fexplorer
To share an Open Graph story:
https://www.facebook.com/dialog/share_open_graph?
app_id=145634995501895
&display=popup
&action_type=og.likes
&action_properties=%7B%22object%22%3A%22https%3A%2F%2Fdevelopers.facebook.com%2Fdocs%2F%22%7D
&redirect_uri=https%3A%2F%2Fdevelopers.facebook.com%2Ftools%2Fexplorer
The 'display=popup' bit affects how the share screen looks, it doesn't open a new window. Possible values for display are:
async, iframe, page, popup, touch, wap