event being fired 3x even with correct js
luizkowalski opened this issue · 10 comments
I have this JS on my app
$(document).on('page:before-change', function(){
if($('.chat-text-field').val())
return confirm("Your message hasn't been sent yet. Do you still want to leave?");
});
turns out that this message is beign fired 3 times. Is there something else missing?
Also, my data-confirm
message are being fired multiple times.
I'm using Ruby 2.2.2 with Rails 4.2.3 and Turbolinks 2.5.3
is it inside $(function() { ... })
?
nope...it is just as I pasted above
is the script loaded inside <body>
? place it on <head>
.
it is placed before the </body>
Sorry for the long delay...that actually worked. Why?
+1
@jlerpscher here is the answer: #51 (comment)
Thank you @luizkowalski but why we have to place it on <head>
?
I asked the same question above hahahaha
Putting the script in the body would defeat the purpose of turbolinks as turbolinks reloads the body but keeps the head to save time.
I think that turbolinks (or jquery-turbolinks) cancels previous handlers etc on page change as the page hasn't actually reloaded according to the browser. Script run in the body though will run each time it is loaded and will stack on top of each other causing events to fire multiple times.