Dont leave page – you have unsaved changes

This is a very cool and small trick to make your users happy!
This code will make sure, that if you make changes in a form and try to leave the page, you will be stopped and asked if you are sure? Very smart.

Only change code line 2 “#some-form” to the form-name, that you want the script to effect.

$(document).ready(function(){
    var form = $('#some-form'),
        original = form.serialize()

    form.submit(function(){
        window.onbeforeunload = null
    })

    window.onbeforeunload = function(){
        if (form.serialize() != original)
            return 'Are you sure you want to leave?'
    }
})