Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 586
Kombo Avatar asked Sep 27 '13 23:09

Kombo


1 Answers

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.

like image 60
Kombo Avatar answered Oct 06 '22 00:10

Kombo