Javascript Alert For Unsaved Changes with Turbolinks, Jquery, And Rails 4
- Categories:
- ruby
- rails
- javascript
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).ready(ready)
$(document).on('page:load', ready)
change = ->
$("form input, form textarea").change( ->
$('a').click( ->
confirm('Unsaved changes on the page. Are you sure?')
)
)
$(document).ready(change)
# 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?');
});
});
};
// ...
Recent Posts
AWS Secrets Manager
Explanation about AWS Secrets Manager with example code.
Envelope Encryption
Envelope encryption is the practice of encrypting plaintext data with a data key, and then encrypting the data key under another key.
Tutorial - "su username vs su - username" - A Security Perspective
The main difference between `su username` and `su - username` lies in the environment variables that are loaded when switching to the specified user.
Subdomain Hijacking
My dormant subdomain was recently hijacked, redirecting it to a online gamble registration page.
C# DbContext ServiceLifeTime
my note about C Sharp ServiceLifeTime