Skip to content

Instantly share code, notes, and snippets.

@mateusneves
Created March 6, 2015 20:58
Show Gist options
  • Select an option

  • Save mateusneves/fd2040ba4556c6db7260 to your computer and use it in GitHub Desktop.

Select an option

Save mateusneves/fd2040ba4556c6db7260 to your computer and use it in GitHub Desktop.
Share buttons function for work with ajax loaded content
function showSocialShareButtons( target ){
/*
Instructions:
This funciton work with ajax load content
<!-- Your HTML -->
<div id="content-to-share">
<div class="share"
data-url="<?php echo $attachment_link; ?>"
data-title="<?php echo $attachment_title; ?>"
data-description="<?php echo $attachment_legend; ?>"
data-image-src="<?php echo $attachment_src; ?>"
data-pinterest-href="https://www.pinterest.com/pin/create/button/?url=<?php echo $attachment_link; ?>&media=<?php echo $attachment_src[0]; ?>&description=<?php echo $attachment_title . ' - ' . $attachment_legend; ?>"
>
<div class="share-buttons"></div>
</div>
</div>
<!-- Now call the function before your content is load -->
showSocialShareButtons( '#content-to-share' );
*/
target = '#' + target;
data_url = $(target + ' .share').attr('data-url');
data_title = $(target + ' .share').attr('data-title');
data_description = $(target + ' .share').attr('data-description');
data_image_src = $(target + ' .share').attr('data-image-src');
data_pinterest_src = $(target + ' .share').attr('data-pinterest-href');
share_butons_html = '<!-- Twitter -->
<div class="share-twitter">
<a href="https://twitter.com/share" class="twitter-share-button" data-url="'+data_url+'" data-text="'+data_title+' - '+data_description+'" data-via="ludopedio" data-lang="pt" data-count="none">Tweetar</a>
</div>
<!-- Facebook -->
<div class="share-facebook">
<div class="fb-like" data-href="'+data_url+'" data-layout="button_count" data-action="like" data-show-faces="true" data-share="false"></div>
</div>
<!-- Google Plus -->
<div class="share-gplus">
<div class="g-plusone" data-size="medium" data-href="'+data_url+'"></div>
</div>
<!-- Pinterest -->
<div class="share-pinterest">
<a href="'+data_pinterest_src+'" data-pin-do="buttonPin" data-pin-config="above"><img src="//assets.pinterest.com/images/pidgets/pinit_fg_en_rect_gray_20.png" /></a>
</div>';
$(target + ' .share-buttons').html( share_butons_html ).promise().done(function(){
//I will assume that if we have one type of button we have them all
//If not we'll just exit
if ($(".twitter-share-button").length == 0) return;
//Twitter
if (typeof (twttr) != 'undefined') {
twttr.widgets.load();
} else {
$.getScript('http://platform.twitter.com/widgets.js');
}
//Facebook
FB.XFBML.parse();
//Google - Note that the google button will not show if you are opening the page from disk - it needs to be http(s)
if (typeof (gapi) != 'undefined') {
$(".g-plusone").each(function () {
gapi.plusone.render($(this).get(0));
});
} else {
$.getScript('https://apis.google.com/js/plusone.js');
}
// Pinterest in new window
$('.share-pinterest a').click(function (e) {
(function (url, title, w, h) {
var left = (screen.width/2)-(w/2);
var top = (screen.height/2)-(h/2);
return window.open(url, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width='+w+', height='+h+', top='+top+', left='+left);
})($(this).attr('href'), 'Pinterest', 600, 280);
e.preventDefault();
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment