Skip to content

Instantly share code, notes, and snippets.

@stowball
Last active October 1, 2015 00:41
Show Gist options
  • Save stowball/9402037 to your computer and use it in GitHub Desktop.
Save stowball/9402037 to your computer and use it in GitHub Desktop.
Lazy loading and triggering the AddThis Share More widget on click of a custom element
<a href="#share" class="share">My custom sharing button</a>
<!-- Put the real addthis Share More link somewhere and hide it -->
<a class="addthis_button_compact display_none"></a>
var customAddthis = {
init: function() {
var $addthisButton = $('.addthis_button_compact');
$('.share').on('click', function(e) {
e.preventDefault();
Modernizr.load({
load: '//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-xxxxxx',
complete: function() {
if (addthis) {
var attempts = 0;
var addthisinterval = window.setInterval(function() {
// AddThis is slow, so we'll wait for a maximum of 15 seconds
if (attempts > 50)
window.clearInterval(addthisinterval);
if ($addthisButton[0].onclick) {
$addthisButton.trigger('click');
window.clearInterval(addthisinterval);
}
attempts++;
}, 300);
}
}
});
});
}
};
customAddthis.init();
@eightdotthree
Copy link

Thanks for this. I made a few updates, primarily to make it more configurable. Update your Gist if you wish.
https://gist.github.com/eightdotthree/29cbf8b2b1243b5deca7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment