Clicking “back” in the browser disables my javascript code if I'm using Turbolinks in Rails

扶醉桌前 提交于 2019-11-28 21:23:45
UT KU

The jquery-turbolinks gem doesn't support Turbolinks 5 anymore, so that solution is deprecated right now.

If you'd like to disable turbolinks-caching, just add this meta to your page;

<meta name="turbolinks-cache-control" content="no-cache">

Two solutions for Turbolinks Classic:

Set the cache to 0 so that no pages are cached using HTML5 History.

Turbolinks.pagesCached(0);

Another solution is to listen to "page:restore" event and call your initialization method. This latter solution is more performant.

document.addEventListener("page:restore", function() {
  app.init();
});

You can find answer in next turbolinks issue discussion. For me, next solution worked - deleting element initialized by JS (in my case chosen dropdown) using next:

document.addEventListener("turbolinks:before-cache", function() {
    $('.chosen-select').chosen('destroy');
})

Instead of completely disabling turbolinks-caching in the whole app (making turbolinks integration quite useless) as detailed in some previous answers, you can just disable turbolinks only for the parts of page that are giving you troubles with JS

<div id="div_using_JS_functions" data-turbolinks='false'>
 .
 .
 .
</div>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!