Search code examples
ruby-on-railsruby-on-rails-4turbolinks

Turbolinks Multiplies Bindings


I'm having an issue where Turbolinks will bind events multiple times when using the back/forward buttons through the browser. I have a simple script to make sure it wasn't the way I have been writing my document.on() events, so...

$(document).on 'page:load', ->
  alert 'Loaded'

Will fire once when clicking a link. Clicking back or forward (or another Turbolink link) it will fire twice. Next time it'll fire a bunch of times and it keeps growing. This is my only thing I have in my manifest file outside of jQuery and Turbolinks. How do you prevent Turbolinks from duplicating event bindings?


Solution

  • The issue seemed to be including the javascript_tag at the end of my body, rather than the head. When in the body, the Javascript was getting removed/added every Turbolinks page:change so the bindings kept piling on.