Last active
April 25, 2018 07:03
-
-
Save Joseph-N/03e17adaf6d30ee24496 to your computer and use it in GitHub Desktop.
How to configure AddThis to work with Turbolinks - Rails 4
This file contains 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
// 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(); | |
}); |
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)
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
Thanks for the workaround.