AddThis is a popular sharing widget that lets you easily implement social sharing in your pages. Implementing AddThis is quite an easy task. All you need to do is to add a script tag in the document and a block of html block consisting of the desired buttons. But the problem is AddThis only initializes items at the domready event. Hence the delayed blocks which were loaded via ajax remains out of reach of AddThis script. So, we need to reinitialize the AddThis instance so that the newly added blocks come to life too. Below is the block of javascript code that will do such a trick:
 

<script>
    var script = 'http://s7.addthis.com/js/250/addthis_widget.js#domready=1';
    if (window.addthis){
        window.addthis = null;
    }
    $.getScript( script , function() { addthis.init(); });
</script>

Add this block at the bottom of your ajax response and you are done. The process is simple. This code block checks if there is a preexisting instance of AddThis already in the window context. If yes, it removes the existing instance and add the script tag dynamically to create a new instance. When the script loads, our script simply calls the init to initialize the AddThis functionality.

Hope this helps! :)

 

Add new comment