How to make the “Share” block load inside <script></script> ?

Works:

 <script type="text/javascript" src="//yastatic.net/share2/share.js" charset="utf-8"> </script> <div class="ya-share2" data-services="vkontakte,facebook,gplus,linkedin"></div> 

Does not work:

 <script type="text/javascript" src="//yastatic.net/share2/share.js" charset="utf-8"> </script> <script type="text/html"> <div class="ya-share2" data-services="vkontakte,facebook,gplus,linkedin"></div> </script> 

    2 answers 2

    Of course, inside the <script> block, valid JavaScript is expected, but not HTML.

    You need to use the block API for javascript :

     var share = Ya.share2('id-элемента', { theme: { services: 'vkontakte,facebook,gplus,linkedin' }, content: { url: 'https://yandex.com' } }); 

    In the user club "Share" they suggested how to solve my problem. I quote:

    The call to Ya.share2 () needs to be postponed until the template engine places the contents of the script tag in the document.

    This is how it works:

     <script type="text/html"> <div id='ya'></div> <script type="text/javascript"> var share = Ya.share2('ya', { theme: { services: 'vkontakte,facebook,gplus,linkedin' }, content: { url: 'example.com' } }); </script> </script>