Skip to main content

Prompt user on unsaved form changes before leaving page.

$(function () {
    var unsaved = false;

    $(document).on(
        "change paste",
        ":input:not(:submit, :reset), textarea, select",
        function () {
            unsaved = true;
        }
    );

    $(":submit, :reset").click(function () {
        unsaved = false;
    });

    function unloadPage() {
        if (unsaved) {
            return "You have unsaved changes, are you sure you want to leave this page?";
        }
    }

    window.onbeforeunload = unloadPage;
});