简体   繁体   中英

Javascript plugins not working properly with turbolinks

I'm using share buttons (javascript plugins). 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. How to make website faster? My website , now plugins is working, because I removed turbolinks.

application.js

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

application.html.erb javascript plugins

<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

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

I tried to add one plugin in javascript file, but result same

$(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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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