Skip to content

Instantly share code, notes, and snippets.

@daveespionage
Created September 20, 2012 11:16
Show Gist options
  • Save daveespionage/3755281 to your computer and use it in GitHub Desktop.
Save daveespionage/3755281 to your computer and use it in GitHub Desktop.
Facebook Post To Feed functionality for Javascript with data attribute overrides
/*global $:false */
/*global FB:false */
function postToFeed(sourceObj) {
// using code from https://developers.facebook.com/docs/reference/dialogs/feed/
// calling the API ...
var obj = {
method: 'feed',
link: '{url}',
picture: '{photo.png}',
name: '{site_name}',
caption: '{site_caption}',
description: '{site_description}'
};
// check for optional overrides to obj
var props = ['description','link','picture'];
var getProp = function(propname,src){
return $(src).attr('data-facebook-' + propname);
};
var setProp = function(propname,propvalue){
if(propvalue !== undefined){
if(propvalue !== ''){
obj[propname] = propvalue;
return true;
}
}
return false;
};
for (var i = 0; i < props.length; i+= 1) {
setProp(props[i],getProp(props[i], sourceObj));
}
function callback(response) {
// handle response
// document.getElementById('msg').innerHTML = "Post ID: " + response['post_id'];
}
FB.ui(obj, callback);
return false;
}
// Initialization of ui elements
// Facebook post functionality
FB.init({appId: "{app_id}", status: true, cookie: true});
$('.facebook-icon').on('click.fbshare',function(){
postToFeed(this);
});
@daveespionage
Copy link
Author

Requires FB api

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment