You must Sign In to post a response.
  • Category: ASP.NET

    Warn user before leaving web page

    Hi,

    I have to warn user before leaving web page with unsaved changes in jquery with custom message in asp.net master page.

    I have tried with beforeunload event, but i want show custom message. I have used below code, but only first time alert message is showing second back button click alert is not showing. can someone help on this.

    var isChange = false;

    $("input").change(function () {
    isChange = true;

    });

    if (window.history && window.history.pushState) {

    $(window).on('popstate', function () {
    if (isChange)
    {
    alert('You have unsaved data on the page.');
    }
    });
    window.history.pushState('forward', null, null);
    }
  • #770007
    please try below code once.

    var isChange = false;

    $("input").change(function () {
    isChange = true;

    });

    $(window).bind('beforeunload', function (e) {

    if(isChange){
    // do raise your warn message

    return false;

    }

    }

    i think, most of the times the window will not retain back but it will definitely warn the user saying that changes are made
    and you will lost if you close the window.

    but its worth in try the above once.i hope it will solve the problem

    Thanks!
    B.Ramana Reddy


  • Sign In to post your comments