我正在尝试在我的Rails站点上设置AddThis插件,但目前仅获得部分成功。问题是它无法正常使用turbo链接。 AddThis共享按钮仅在页面刷新后出现。如果我单击网站的其他部分,AddThis工具栏将消失。如果我禁用涡轮链接,那么它将在站点的每个部分都起作用。
这是我生成智能图层时获得的代码:
<!-- AddThis Smart Layers BEGIN -->
<!-- Go to http://www.addthis.com/get/smart-layers to customize -->
<script type="text/javascript" src="//s7.addthis.com/js/300/addthis_widget.js#pubid=xxx"></script>
<script type="text/javascript">
addthis.layers({
'theme' : 'transparent',
'share' : {
'position' : 'left',
'numPreferredServices' : 5
}
});
</script>
<!-- AddThis Smart Layers END -->
该代码当然不适用于turbo链接。我找到了该解决方案的how to make add this work with turbilinks rails 4,作者确认该解决方案有效,但是当我单击该网站的其他部分时,我仍然遇到问题。
我试图将此代码插入
head
文件中的application.html.erb
标记内:<!-- AddThis Smart Layers BEGIN -->
<!-- Go to http://www.addthis.com/get/smart-layers to customize -->
<script type="text/javascript">$(document).ready(function() {
var script="//s7.addthis.com/js/300/addthis_widget.js#pubid=xxx";
if (window.addthis){
window.addthis = null;
window._adr = null;
window._atc = null;
window._atd = null;
window._ate = null;
window._atr = null;
window._atw = null;
}
$.getScript(script, function(){
addthis.layers({
'theme' : 'transparent',
'share' : {
'position' : 'left',
'numPreferredServices' : 5
}
});
});
});
</script>
<!-- AddThis Smart Layers END -->
打开网站后,AddThis通常会再次加载,但是一旦切换到网站的其他部分,AddThis就会消失。
如何在启用Turbo链接的情况下使该脚本在网站的每个部分上正常工作?
编辑1:
也尝试了这个但没有成功:
<script type="text/javascript">
$(document).on('page:change', 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("//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-xxx");
});
</script>
最佳答案
您是否尝试过:
#app/assets/javascripts/application.js
addthis = 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("//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-xxx");
}
$(document).ready(addthis);
$(document).on('page:load', addthis);
$(document).on('page:change', addthis);
关于javascript - 如何使AddThis与Rails 4 Turbo-link一起使用?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21764990/