I have a Rails 4 app, which uses Turbolinks
. My understanding is that Turbolinks
breaks jQuery code, as Turbolinks does not load new page, but only get new elements.
Therefore, navigating to new page may not trigger .ready
, although it always triggers .page:load
, and thus new jQuery code won't initialize.
I have a lot of jQuery code, so I don't want to modify my jQuery code to be compatible with Turbolinks.
Is it possible to add a javascript code to my application.js that overwrites .ready
event to include page:load
as well? How should I do it?
i had to use the
page:change
event:js:
coffee script:
Rather than wait for
$(document).ready
to fire for your jQuery, just usepage:load
instead:Alternatively, you can set up the jquery.turbolinks gem: https://github.com/kossnocorp/jquery.turbolinks
With TurboLinks 5 / Rails 5 ... I would recommend instantiating DataTables like this.
It will prevent the heading and footer paging from showing up multiple times when the back button is used.
With turbolinks 5.0.0, the events changed to
turbolinks:load
. See full list of turbolinks events.The documentation recommends following code:
The jquery.turbolinks fork located at https://github.com/dalpo/jquery.turbolinks already reflects these changes and allows for a seamless drop-in of turbolinks. Nevertheless, I would go for the
turbolinks:load
event to have full control and not require another library.