Click here to Skip to main content
16,021,285 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

In my application I have a chart to save. If some one fill the chart and didn't save the values it should produce a pop up to show him/her that data has not been saved.

I did it like...

On
window.onbeforeunload
I am checking the ID of value field(which is stored in a hidden field) is saved or not from database. And according to that I am showing the leave page message or not.

JavaScript
<script>
        window.onbeforeunload = function (evt) {
            /* get the scenario id first. */
            var id1 = document.getElementById("<%= hdScenario1.ClientID %>").value;
            var id_1='';

            $.ajax({
                type: "POST",
                url: "scenarioAdd.aspx/CheckUpdateScenario",
                data: '{scenarioId: ' + id1 + '}',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (response) {
                    id_1 = response.d;

                    if (id_1 == 0) {
                        var message = 'Save it before you close.';

                        if (typeof evt == 'undefined') {
                            evt = window.event;
                        }
                        if (evt) {
                            evt.returnValue = message;
                        }
                        return message;
                    }
                },
                failure: function (response) {
                    alert(response.d + " f");
                    id_1 = "0";
                },
                error: function (response) {
                    alert(response.d + " e");
                    id_1 = "0";
                }
            });

            // leave page code was before here.
        }
    </script>



The main lines
JavaScript
var message = &#39;Save it before you close.&#39;;

if (typeof evt == &#39;undefined&#39;) {
     evt = window.event;
}
if (evt) {
   evt.returnValue = message;
}
return message;


was written on the comment section above. Then it was running ok. but this part is executing before ajax. so the ultimate result is null. Then I put the code into ajax success. And after this its not working.

Kindly check the issue and if possible fixed it.

Thanks..
Posted

1 solution

The problem is that you're issuing an asynchronous call to the server. The beforeunload event handler returns before the AJAX call has completed, and the browser continues with unloading the page.

According to the documentation[^], you can specify async:false in the settings passed to $.ajax(...), which might solve the problem:

JavaScript
window.onbeforeunload = function (evt) {
    /* get the scenario id first. */
    var id1 = document.getElementById("<%= hdScenario1.ClientID %>").value;
    var id_1 = '';
    
     $.ajax({
         type: "POST",
         url: "scenarioAdd.aspx/CheckUpdateScenario",
         data: '{scenarioId: ' + id1 + '}',
         contentType: "application/json; charset=utf-8",
         dataType: "json",
         
         // Make a synchronous request:
         async: false,
         
         success: function (response) {
             id_1 = response.d;
         },
         failure: function (response) {
             alert(response.d + " f");
             id_1 = 0;
         },
         error: function (response) {
             alert(response.d + " e");
             id_1 = 0;
         }
    });
    
    if (id_1 == 0) {
        var message = 'Save it before you close.';
        if (typeof evt == 'undefined') { 
            evt = window.event;
        }
        if (evt) {
            evt.returnValue = message;
        }
        
        return message;
    }
};
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900