Skip to content

Instantly share code, notes, and snippets.

@Joseph-N
Last active April 25, 2018 07:03
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • 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();
});
@senott
Copy link

senott commented Oct 2, 2015

Thanks, man!

@ChrisEstanol
Copy link

Thanks for the workaround.

@trantorLiu
Copy link

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