4

I am using the following code which runs fine in Internet Explorer but is not working in Firefox.

When the user closes the browser, a webmethod has to be called, which updates the bit field IsLogin=false in the database.

window.onbeforeunload = function (e) {

        var evt = window.event || e;
        var y = evt.clientY || evt.pageY;

        if (y < 0 || evt.clientX<0) {
            $.ajax({
                type: "POST",
                url: "/Application/WebForm1.aspx/Update",
                async: false,
                data: "{}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {
                    alert('Sucessfull Call');
                }

            });
        }
    }
AShelly
  • 34,686
  • 15
  • 91
  • 152
  • Probably no reliable way to do this as mentioned here: http://stackoverflow.com/questions/2970782/javascript-wait-until-ajax-request-finishes-to-close-page – Dan Mar 12 '13 at 15:06

1 Answers1

0

Is your web method getting called at all in FireFox? The HTML5 specification states that calls to alert() may be ignored during the window.onbeforeunload event see here for Mozilla's response.

An alternative is to return a confirmation box. This appears to work well in IE/FF/Chrome for me. The document is still visible and the event can be cancelled (if needed).

<script type="text/javascript">

    window.addEventListener("beforeunload", function (e) {
        var confirmationMessage = "Sucessfull Call";

        (e || window.event).returnValue = confirmationMessage;
        return confirmationMessage;
    });
</script>
Aaron B
  • 31
  • 3