Skip to content

Instantly share code, notes, and snippets.

@Joseph-N
Last active Apr 25, 2018
Embed
What would you like to do?
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();
});
@senott

This comment has been minimized.

Copy link

@senott senott commented Oct 2, 2015

Thanks, man!

@ChrisEstanol

This comment has been minimized.

Copy link

@ChrisEstanol ChrisEstanol commented Feb 18, 2016

Thanks for the workaround.

@trantorLiu

This comment has been minimized.

Copy link

@trantorLiu trantorLiu commented Jan 21, 2017

It works. But when page loads, some error occur:

Uncaught ReferenceError: _atr is not defined
    at addthis_widget.js?_=1484985323526:2
Uncaught ReferenceError: addthis is not defined
    at _handleScroll (addthis_widget.js?_=1484985323526:2)
@nhinze

This comment has been minimized.

Copy link

@nhinze 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