Hello.
On the site I make a music album. I decided for each track to make the buttons "Like".
With a button from VKontakte, everything is very simple:

<div id="vk_like_[id]"></div> <script type="text/javascript"> VK.Widgets.Like("vk_like_[id]", { type: "mini", pageTitle: 'Заголовок', pageDescription: 'Мне понравился трек [trackname]'}, [track_id]); </script> 

That is, what is the point, in the loop, you can create any number of containers with a unique ID , and in the same cycle generate JS, send like ID to the ID container you need and at the end we specify a 32-bit number (in our case, track_id ), which makes many like buttons on one page unique.

In FaceBook, I can't figure out how to do this. Who has already done this, do not know how to do it?
Thank. :)

  • and if the site is dynamic and on all the buttons FB-> like posts are the same for being clicked? - user17465

3 answers 3

Well, if I understand correctly, then add the JavaScript SDK once on the 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/ru_RU/all.js#xfbml=1"; fjs.parentNode.insertBefore(js, fjs); }(document, 'script', 'facebook-jssdk'));</script> 

They write that it is ideal to do this immediately after opening <body>. And then you can add as many buttons as you want:

 <div class="fb-like" data-href="http://myurl" data-send="false" data-layout="button_count" data-width="450" data-show-faces="true"></div> 

Where respectively http: // myurl is a url for each of your tracks.

Read more

Addition.

To separate the tracks on the page, do anchor by id. For example,

 <div id="track1">Тут Ваш трек1</div> <div id="track2">Тут Ваш трек2</div> 

And in the buttons use http: // myurl # track1 and http: // myurl # track2. True, he did not check, I do not know how it will work.

  • I have all the tracks on one page www.myurl.com/home/audios that is, the track separately link does not have. Well, except that a direct link to the mp3 file itself. - Eugene
  • Yes, I said the same thing - one to one :) - Ozim
  • Ozim, I do not argue, people sometimes converge thoughts. I answered a little later, but when I started to answer your answer was not yet. Eugene, see addition, if you can accomplish your goal, please, for future generations;) - invincible
  • Yes it works. :) - Eugene

this is after badi

  <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/ru_RU/all.js#xfbml=1"; fjs.parentNode.insertBefore(js, fjs); }(document, 'script', 'facebook-jssdk'));</script> 

it is in a loop, substituting the necessary reference in the data-href

 <div class="fb-like" data-href="site.ru" data-send="true" data-width="450" data-show-faces="true"></div> 
  • Hmm, what if I have all the tracks on the same page? - Eugene
  • What's the difference. There is a data-href parameter - you push the necessary url into it and that's it. - Ozim
  • @eugene, and then what should people like? i like is a feature that shows that someone likes some kind of url, so make your own links so they can be liked. Yes, enter a direct link to the file. - Ozim
  • well, OK. "Like" VKontakte you can specify any text. that is, what exactly I liked is admissible. But all these buttons can lead to the same link. and there is already a list of tracks. and against each track there is a “Like” button and EACH button has a counter. well and everything. I need it. and in FB it is necessary to point to every single link, so I understood that the counter counts correctly. - Eugene
  • pancake! Like can not a piece of code on the page! Like may link! make links site.ru/track.mp3 and you will have different data on each track, if you have them go only as a list, and if each track also has its own page - then make a link to this page, for example site.ru/ trach.mp3.html what is not clear here? - Ozim pm

As far as I know, you need to do them in frames. An example can be found at http://droidnews.ru/ . But only such goodness sometimes so well slows down the page, that I want to send these programmers away from the mountain, and close the site.

  • I do not know how FB, but VC buttons do not slow down the page. Thanks, I'll take a look. - Eugene
  • frames are not very good, especially if there will be a lot of them on the page - invincible
  • VK buttons still slow down the page. But it manifests itself if there is not registered. - KoVadim