【发布时间】:2014-02-13 20:39:33
【问题描述】:
我正在尝试在我的 Rails 网站上设置 AddThis 插件,但目前只取得了部分成功。问题是它不能像涡轮链接那样工作。 AddThis 分享按钮仅在页面刷新后出现。如果我单击站点的其他部分,AddThis 工具栏就会消失。如果我禁用 turbo-links,那么它将在网站的每个部分都有效。
这是我生成智能层时得到的代码:
<!-- 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-links。我找到了这个解决方案'how to make add this work with turbilinks rails 4,作者确认它有效,但是当我点击网站的其他部分时我仍然遇到问题。
我尝试将此代码插入到我的application.html.erb 文件中的head 标记中:
<!-- 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>
【问题讨论】:
标签: javascript ruby-on-rails addthis turbolinks