Skip to content

Instantly share code, notes, and snippets.

@Joseph-N
Last active April 25, 2018 07:03
Show Gist options
  • Save Joseph-N/03e17adaf6d30ee24496 to your computer and use it in GitHub Desktop.
Save Joseph-N/03e17adaf6d30ee24496 to your computer and use it in GitHub Desktop.
How to configure AddThis to work with Turbolinks - Rails 4
// turbolinks addthis
var initAdthis;
initAdthis = function(){
// Remove all global properties set by addthis, otherwise it won't reinitialize
for (var i in window) {
if (/^addthis/.test(i) || /^_at/.test(i)) {
delete window[i];
}
}
window.addthis_share = null;
// Finally, load addthis
$.getScript("http://s7.addthis.com/js/300/addthis_widget.js#pubid=YOUR-PUBLISHER-ID");
}
// Trigger the function on both jquery's ready event and turbolinks page:change event
$(document).on('ready page:change', function() {
initAdthis();
});
@nhinze
Copy link

nhinze commented Mar 23, 2017

I'm having the same problem. First I changed:

$(document).on('ready page:change', function() {

to

$(document).on('page:change', function() {

otherwise initAdthis is called twice on first page load.

However, the _atr is not defined error multiplies as I browse through the website. I think AddThis adds an event that needs to be removed before page:change.

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