How to show the “Are you sure you want to navigate away from this page?” when changes committed?

前端 未结 17 1573
深忆病人
深忆病人 2020-11-22 02:11

Here in stackoverflow, if you started to make changes then you attempt to navigate away from the page, a javascript confirm button shows up and asks: \"Are you sure you want

相关标签:
17条回答
  • 2020-11-22 03:01

    The standard states that prompting can be controlled by canceling the beforeunload event or setting the return value to a non-null value. It also states that authors should use Event.preventDefault() instead of returnValue, and the message shown to the user is not customizable.

    As of 69.0.3497.92, Chrome has not met the standard. However, there is a bug report filed, and a review is in progress. Chrome requires returnValue to be set by reference to the event object, not the value returned by the handler.

    It is the author's responsibility to track whether changes have been made; it can be done with a variable or by ensuring the event is only handled when necessary.

    window.addEventListener('beforeunload', function (e) {
        // Cancel the event as stated by the standard.
        e.preventDefault();
        // Chrome requires returnValue to be set.
        e.returnValue = '';
    });
        
    window.location = 'about:blank';

    0 讨论(0)
  • 2020-11-22 03:05

    jquerys 'beforeunload' worked great for me

    $(window).bind('beforeunload', function(){
        if( $('input').val() !== '' ){
            return "It looks like you have input you haven't submitted."
        }
    });
    
    0 讨论(0)
  • 2020-11-22 03:05

    To expand on Keith's already amazing answer:

    Custom warning messages

    To allow custom warning messages, you can wrap it in a function like this:

    function preventNavigation(message) {
        var confirmOnPageExit = function (e) {
            // If we haven't been passed the event get the window.event
            e = e || window.event;
    
            // For IE6-8 and Firefox prior to version 4
            if (e)
            {
                e.returnValue = message;
            }
    
            // For Chrome, Safari, IE8+ and Opera 12+
            return message;
        };
        window.onbeforeunload = confirmOnPageExit;
    }
    

    Then just call that function with your custom message:

    preventNavigation("Baby, please don't go!!!");
    

    Enabling navigation again

    To re-enable navigation, all you need to do is set window.onbeforeunload to null. Here it is, wrapped in a neat little function that can be called anywhere:

    function enableNavigation() {
        window.onbeforeunload = null;
    }
    

    Using jQuery to bind this to form elements

    If using jQuery, this can easily be bound to all of the elements of a form like this:

    $("#yourForm :input").change(function() {
        preventNavigation("You have not saved the form. Any \
            changes will be lost if you leave this page.");
    });
    

    Then to allow the form to be submitted:

    $("#yourForm").on("submit", function(event) {
        enableNavigation();
    });
    

    Dynamically-modified forms:

    preventNavigation() and enableNavigation() can be bound to any other functions as needed, such as dynamically modifying a form, or clicking on a button that sends an AJAX request. I did this by adding a hidden input element to the form:

    <input id="dummy_input" type="hidden" />
    

    Then any time I want to prevent the user from navigating away, I trigger the change on that input to make sure that preventNavigation() gets executed:

    function somethingThatModifiesAFormDynamically() {
    
        // Do something that modifies a form
    
        // ...
        $("#dummy_input").trigger("change");
        // ...
    }
    
    0 讨论(0)
  • 2020-11-22 03:07

    This is an easy way to present the message if any data is input into the form, and not to show the message if the form is submitted:

    $(function () {
        $("input, textarea, select").on("input change", function() {
            window.onbeforeunload = window.onbeforeunload || function (e) {
                return "You have unsaved changes.  Do you want to leave this page and lose your changes?";
            };
        });
        $("form").on("submit", function() {
            window.onbeforeunload = null;
        });
    })
    
    0 讨论(0)
  • 2020-11-22 03:08

    When the user starts making changes to the form, a boolean flag will be set. If the user then tries to navigate away from the page, you check that flag in the window.onunload event. If the flag is set, you show the message by returning it as a string. Returning the message as a string will popup a confirmation dialog containing your message.

    If you are using ajax to commit the changes, you can set the flag to false after the changes have been committed (i.e. in the ajax success event).

    0 讨论(0)
提交回复
热议问题