ShareThis Asynchronous Javascript Loading

Unfortunately the ShareThis service has a lot of known issues, like loading a lot of data from their tracking site, b.scorecardresearch.com and other types of bloat when initializing their icons.

Because calling stLight.options depends on having the buttons.js script already loaded, it is not really feasible to initialize the code like this:

<script type="text/javascript" src="http://w.sharethis.com/button/buttons.js"></script>
<script type="text/javascript">stLight.options({publisher: "6beba854-ee6d-4ae1-a4f3-b69815c8ef63"});</script>

However, you could use an onload function to do the stLight.options, and then wrap the entire thing inside of a function that won’t interfere with the global namespace, like this:

<script>
// ShareThis Asynchronous Javascript Loading
// from https://techblog.willshouse.com/?p=5265
(function(){
    // window.switchTo5x=false; // (optional)
    var e=document.createElement("script");
    e.type="text/javascript";
    e.async=true;
    e.onload=function(){
        try{
            stLight.options({publisher: "6beba854-ee6d-4ae1-a4f3-b69815c8ef63", doNotHash: true, doNotCopy: true, hashAddressBar: false});
        }catch(e){ /* optionally do something to handle errors here */ }
    }
    e.src=('https:' == document.location.protocol ? 'https://ws' : 'http://w') + '.sharethis.com/button/buttons.js';
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(e, s);
})();
</script>

Make sure to update your publisher ID in the code above if you copy / paste it.

Related Posts:

This entry was posted in Social Media, Tech Tips and tagged . Bookmark the permalink.

2 Responses to ShareThis Asynchronous Javascript Loading

  1. Paolo says:

    Thanks a lot. It works great.

  2. Randy says:

    Hey, this works great. A few notes on implementation:

    1. Use the above to replace the sharethis code that goes in the tags.
    2. The function needs to be wrapped in tags.
    3. Don’t forget to replace the publisher ID with your own.

    Worked on the first try, so I wanted to thank you.

Leave a Reply

Your email address will not be published. Required fields are marked *