简体   繁体   English

Javascript插件无法正常使用turbolinks

[英]Javascript plugins not working properly with turbolinks

I'm using share buttons (javascript plugins). 我正在使用共享按钮(javascript插件)。 When I open a page it shows that buttons, but when I open other page it disappear. 当我打开一个页面时,它会显示按钮,但是当我打开其他页面时它会消失。 It appear again when i refresh website. 当我刷新网站时它再次出现。 It working properly with jquery.turbolinks and without turbolinks gem, but without it website is very slow. 它与jquery.turbolinks和没有turbolinks宝石正常工作,但没有它网站是非常缓慢的。 How to make website faster? 如何让网站更快? My website , now plugins is working, because I removed turbolinks. 我的网站 ,现在插件正在工作,因为我删除了turbolinks。

application.js 的application.js

//= require jquery
//= require jquery.turbolinks
//= require jquery_ujs
//= require lightbox
//= require_tree .
//= require turbolinks

application.html.erb javascript plugins application.html.erb javascript插件

<script type="text/javascript" src="//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-588e44a9401a6d9f"></script>
<script type="text/javascript" id="st_insights_js" src="http://w.sharethis.com/button/buttons.js?publisher=630cdfb3-b6c5-4da7-a389-1c7d5340827d"></script>
<script type="text/javascript">stLight.options({publisher: "630cdfb3-b6c5-4da7-a389-1c7d5340827d", doNotHash: false, doNotCopy: false, hashAddressBar: false});</script>

Gemfile 的Gemfile

gem 'jquery-turbolinks'
gem 'turbolinks', '~> 5.0.1'

I tried to add one plugin in javascript file, but result same 我试图在javascript文件中添加一个插件,但结果相同

$(document).on('turbolinks:load', function(){
  stLight.options({publisher: "630cdfb3-b6c5-4da7-a389-1c7d5340827d",      doNotHash: false, doNotCopy: false, hashAddressBar: false});
});

I think you need to re-initialize the buttons, since the buttons.js script is not re-inserted through turbolinks... I'd try something like this: 我认为你需要重新初始化按钮,因为buttons.js脚本没有通过turbolinks重新插入...我会尝试这样的事情:

$(document).on('turbolinks:load', function(){
    stLight.readyRun = false;
    stLight.onReady(); // re-initialization
});

Hope this helps! 希望这可以帮助!

Have you tried to add something like that? 你试过添加类似的东西吗?

// 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();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM