Last active
October 1, 2015 00:41
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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