4

我正在使用以下代码,它在 Internet Explorer 中运行良好,但在 Firefox 中无法运行。

当用户关闭浏览器时,必须调用一个 web 方法来更新IsLogin=false数据库中的位字段。

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');
                }

            });
        }
    }
4

1 回答 1

0

您的 Web 方法是否在 FireFox 中被调用?HTML5 规范指出,在 window.onbeforeunload 事件期间可能会忽略对 alert() 的调用,请参阅此处了解Mozilla 的响应。

另一种方法是返回确认框。对我来说,这似乎在 IE/FF/Chrome 中运行良好。文档仍然可见,并且可以取消事件(如果需要)。

<script type="text/javascript">

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

        (e || window.event).returnValue = confirmationMessage;
        return confirmationMessage;
    });
</script>
于 2013-06-06T16:24:29.540 回答