Update 2020-04-04: This script based on old version Coffescript, jQuery, and Turbolinks circa 2013.

jQuery change event can be used to detect form change. But because Turbolinks pages will change without full reload, we can’t rely on DOMContentLoaded or jQuery.ready() to trigger change event. Here is how to write jQuery change event code with Turbolinks enabled. Tested with Rails 4.0.0. Github Gist.

# Coffeescript

ready = >
  # your jquery code
  # ...

# page:load is fired at the end of the loading process.
$(document).on('page:load', ready)

change = ->
  $("form input, form textarea").change( ->
    $('a').click( ->
      confirm('Unsaved changes on the page. Are you sure?')


# the page has been parsed and changed to the new version and on DOMContentLoaded
$(document).on('page:change', change)
// Javascript

// ...
change = function() {
  $("form input, form textarea").change(function() {
    $('a').click(function() {
      confirm('Unsaved changes on the page. Are you sure?');

// ...