When I click on the news headline, I try to update the content of the dialog box so that when I click on the "Share" button, the content of the selected news will appear in the dialog box.

The problem is that when you click on a button, several dialog boxes are called at once.

Here is the function code that displays the news and updates the content of the social buttons. networks:

<div id="ya-share1" class="ya-share2" data-services="vkontakte,facebook,odnoklassniki"></div> $(document).on('click', '.news', function () { selectNews(); }); function selectNews() { $.post('php/select_news.php').done(function (data) { var share1 = Ya.share2('ya-share1', { content: { url: 'http://babys-joy.ru/news' } }); share1.updateContent({ url: 'http://babys-joy.ru/news' }); }); } 

    1 answer 1

    In your case, the block is initialized with each call to the Ya.share2 function.

    Initialization is sufficient to perform once, and in the function body, leave only the call to the content update method:

     var share1 = Ya.share2('ya-share1', { content: { url: 'http://babys-joy.ru/news' } }); function selectNews() { $.post('php/select_news.php').done(function (data) { share1.updateContent({ url: 'http://babys-joy.ru/news' }); }); } 
    • did as you advised. However, it still causes two windows. I also noticed one feature: if I prescribe the class ya-share1 instead of ya-share2 in a block with buttons, then everything seems to be normal - one window is called. - amijin
    • @amijin The ya-share2 used for automatic initialization. If you need to dynamically change the content (and you initialize the block through the API), you do not need to specify this class. - Roman Paradeev