[英]Javascript plugins not working properly with turbolinks
我正在使用共享按鈕(javascript插件)。 當我打開一個頁面時,它會顯示按鈕,但是當我打開其他頁面時它會消失。 當我刷新網站時它再次出現。 它與jquery.turbolinks和沒有turbolinks寶石正常工作,但沒有它網站是非常緩慢的。 如何讓網站更快? 我的網站 ,現在插件正在工作,因為我刪除了turbolinks。
的application.js
//= require jquery
//= require jquery.turbolinks
//= require jquery_ujs
//= require lightbox
//= require_tree .
//= require turbolinks
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
gem 'jquery-turbolinks'
gem 'turbolinks', '~> 5.0.1'
我試圖在javascript文件中添加一個插件,但結果相同
$(document).on('turbolinks:load', function(){
stLight.options({publisher: "630cdfb3-b6c5-4da7-a389-1c7d5340827d", doNotHash: false, doNotCopy: false, hashAddressBar: false});
});
我認為你需要重新初始化按鈕,因為buttons.js腳本沒有通過turbolinks重新插入...我會嘗試這樣的事情:
$(document).on('turbolinks:load', function(){
stLight.readyRun = false;
stLight.onReady(); // re-initialization
});
希望這可以幫助!
你試過添加類似的東西嗎?
// 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.