我正在尝试在我的Rails网站上设置AddThis插件,但目前只取得了部分成功。问题是它不能像turbo-links一样工作。 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,其中作者确认它有效,但当我点击该网站的其他部分时我仍然遇到问题。
我尝试在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-links的情况下让该脚本在我的网站的每个部分上运行?
编辑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>
答案 0 :(得分:5)
你试过这个:
#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);
答案 1 :(得分:3)
此解决方案不需要事件处理程序。
首先,我在app/views/layouts/_add_this.html.erb
创建了部分内容:
<script type="text/javascript">
if (window.addthis) {
window.addthis = null;
window._adr = null;
window._atc = null;
window._atd = null;
window._ate = null;
window._atr = null;
window._atw = null;
}
</script>
<script src="http://s7.addthis.com/js/300/addthis_widget.js#pubid=XXXXXXXXX" type="text/javascript"></script>
然后,每当我需要添加它时,我都会使用:
<%= render 'layouts/add_this' %>
出于某种原因,getScript
对我不起作用。
答案 2 :(得分:0)
这是唯一对我有用的东西:
调用addthis时这样做:
<script type="text/javascript" src="//s7.addthis.com/js/300/addthis_widget.js#pubid=<YOUR PUB ID HERE>"></script>
<!-- Go to www.addthis.com/dashboard to customize your tools -->
<div class="addthis_sharing_toolbox"></div>
确保在application.js文件中的turbolinks和jQuery之后包含此文件:
<强> addthis.js 强>
//fixes social addthis links
var addthis;
addthis = function() {
var script = 'http://s7.addthis.com/js/300/addthis_widget.js#pubid=<YOUR PUB ID HERE>#async=1';
// 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;
// Remove all global properties set by addthis, otherwise it won't reinitialize
if(window.addthis) {
window.addthis = null;
window._adr = null;
window._atc = null;
window._atd = null;
window._ate = null;
window._atr = null;
window._atw = null;
}
// Finally, load addthis
$.getScript(script, function(){
addthis.toolbox('.addthis_toolbox');
});
}
$(document).ready(addthis);
$(document).on('page:load', addthis);
$(document).on('page:change', addthis);
答案 3 :(得分:0)
删除addthis js的turbolinks效果。您可以使用jquery-turbolinks gem。你只需要按照指南安装gem然后放入application.js jquery-turbolinks就像这样:
//= require jquery
//= require jquery.turbolinks
//= require jquery_ujs
//
// ... your other scripts here ...
//
//= require turbolinks
此Turbolinks将完美地用于页面加载,但允许我们的js和jquery在turbolinks生效之前加载。
您可以通过此blog post来查看此gem的使用详情。