K
K
Kira2016-02-20 23:26:42
Facebook
Kira, 2016-02-20 23:26:42

How to create a "share" button in facebook?

Hello.
I'm trying to create three "share" buttons facebook, twitter and vk, but the popup window only works for the last two. On Facebook, it opens, but when you enter the URL and click on the button, nothing happens.
Here is the code

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Nemiro, 2016-02-21
@Allan11

The address must be specified. In your example, the text "URL" is passed instead of the address.
You can make parameter passing through an object, for flexibility:

Share = {
  getParams: function(params) {
    // используем полученные параметры, 
    // либо создаем пустой объект, чтобы не было ошибок
    params = params || {};
    // в качестве url используем params.url,
    // либо адрес текущей страницы (window.location.href), если params.url не указан
    params.url = params.url || window.location.href;
    // используем params.title, либо заголовок документа
    params.title = params.title || document.title;
    // и т.п.
    params.description = params.description || '';
    params.img = params.img || '';

    return params;
  },

  vkontakte: function(params) {
    params = Share.getParams(params);
    url = 'http://vkontakte.ru/share.php?';
    url += 'url=' + encodeURIComponent(params.url);
    url += '&title=' + encodeURIComponent(params.title);
    url += '&description=' + encodeURIComponent(params.description);
    url += '&image=' + encodeURIComponent(params.img);
    url += '&noparse=true';
    Share.popup(url);
  },

  facebook: function(params) {
  	params = Share.getParams(params);
    url = 'http://www.facebook.com/sharer.php?s=100';
    url += '&p[title]=' + encodeURIComponent(params.title);
    url += '&p[summary]=' + encodeURIComponent(params.description);
    url += '&p[url]=' + encodeURIComponent(params.url);
    url += '&p[images][0]=' + encodeURIComponent(params.img);
    Share.popup(url);
  },

  twitter: function(params) {
    params = Share.getParams(params);
    url = 'http://twitter.com/share?';
    url += 'text=' + encodeURIComponent(params.description);
    url += '&url=' + encodeURIComponent(params.img);
    url += '&counturl=' + encodeURIComponent(params.img);
    Share.popup(url);
  },

  popup: function(url) {
    window.open(url, '', 'toolbar=0,status=0,width=626,height=436');
  }
};

<span title="Поделиться в Facebook">
  <a onclick="Share.facebook({url: 'https://toster.ru/q/294480'})">
    <i class="facebook square icon">Facebook</i>
  </a>
</span>
<span title="Поделиться в Twitter">
  <a onclick="Share.twitter({description: 'Hello world!'})">
    <i class="twitter square icon">Twitter</i>
  </a>
</span>
<span title="Поделиться ВКонтакте">
  <a onclick="Share.vkontakte({url: 'https://toster.ru/q/294480', description: 'Ответ на вопрос', 
title: 'Как правильно создать кнопку «поделиться» в facebook?'})">
  <i class="vk icon">VK</i>
  </a>
</span>

View .
If memory serves, Facebook has recently (or has long been) ignoring the transmitted descriptions and page titles and extracting the necessary information on its own, directly from the specified link (mainly from Open Graph tags ).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question