This custom Facebook share button works great when you place just one button on each page. (This is an excellent way to make projects made in Ajax and JavaScript traceable to search engines and social networks.)
When I put multiple buttons on the same page I had problems with querySelector
(In the documentation it says that querySelector
returns the first element inside the document ...), but I still do not know how to return only the element that was clicked.
I would like a well-rounded orientation to complete part of this long search, to make shares buttons traceable.
@Sergio Thanks for the introduction in the comment, but I still could not match the querySelectorAll
with the iteration with the for
, so I concatenei the question as directed.
This is the part corresponding to the source file, edited and made available to my project, you can see the original post on this site: customizable Facebook share button
Html
<div id="fb-root"></div>
<a fb-share ng-href="http://www.exemplo.com/{{item.href}}" data-image="{{item.img}}" data-title="{{item.titulo}}" data-desc="{{item.descricao}}" class="fb_share" >
<div class="botao-fb"></div>
</a>
CSS: to insert the image in a more practical way
.botao-fb {
width: 101px;
height: 20px;
background-image:url(/images/fb-icone.png);
background-repeat:no-repeat;
margin-bottom: 3px;
}
Policy with the script
.directive('fbShare', ['$window', function ($window) {
return {
scope:{
fbShare: '='
},
restrict: 'AE',
link: function (scope, element, attrs) {
window.fbAsyncInit = function() {
FB.init({
appId: '1405211842504456', //esse AppId é apenas demonstrativo, substitua com o seu
status: true,
cookie: true,
xfbml: true
});
};
(function(d, debug){var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];if (d.getElementById(id)) {return;}js = d.createElement('script'); js.id = id; js.async = true;js.src = "//connect.facebook.net/pt_BR/all" + (debug ? "/debug" : "") + ".js";ref.parentNode.insertBefore(js, ref);}(document, /*debug*/ false));
function postToFeed(title, desc, url, image) {
var obj = {method: 'feed',link: url, picture: 'http://www.exemplo.com/images/'+image,name: title,description: desc};
function callback(response) {}
FB.ui(obj, callback);
}
var fbShareBtn = document.querySelectorAll('.fb_share');
for (var i = 0; i < fb_share.length; i++) {
console.log(fb_share[i]);
};
fbShareBtn.addEventListener('click', function(e) {
e.preventDefault();
var title = fbShareBtn.getAttribute('data-title'),
desc = fbShareBtn.getAttribute('data-desc'),
url = fbShareBtn.getAttribute('href'),
image = fbShareBtn.getAttribute('data-image');
postToFeed(title, desc, url, image);
return false;
});
}]);