AlexB AlexB - 2 months ago 6
Javascript Question

Twitter button script loaded synchronously, need to load it asynchronously

I having little trouble with twitter button script, which is blocking the page rendering.

The script I have been using is the following

<a href="https://twitter.com/share" class="twitter-share-button" data-url="$info['referring_url']" data-count="vertical" data-via="MyTwitterID">Tweet</a>
<script type="text/javascript">!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>


Any ides on how to get this one sorted?

Answer

Actually, I have found the way myself,

for those whom might need to know, simply add the following:

js.async=true;

so the line should look like this:

<a href="https://twitter.com/share" class="twitter-share-button" data-url="$info['referring_url']" data-count="vertical" data-via="MyTwitterID">Tweet</a>
<script type="text/javascript">!function(d,s,id){
var js,fjs=d.getElementsByTagName(s)[0];
if(!d.getElementById(id)){
    js=d.createElement(s);js.id=id;js.async=true;
    js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);
}
}(document,"script","twitter-wjs");</script>