浏览器关闭时的调用控制器方法 [英] Call controller method on browser close

查看:303
本文介绍了浏览器关闭时的调用控制器方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我已经将 windows.onbeforeunload 实现为


I have implemented windows.onbeforeunload as.

// Body tag is as
<body  önmousedown="BodyClicked()">

window.onbeforeunload = WindowCloseHanlder;
    var isClose = false;
    function WindowCloseHanlder() {
//        alert(document.getElementById(''a_TestMe'').title);
        if (!isClose) {
                 $.ajax({
                    type: "GET",
                    url: "/Account/LogOff" // the URL of the controller action method
                });
        }
    }
    function BodyClicked() {
        isClose = true;
    }


仅当我关闭选项卡但不直接关闭浏览器时才调用 LogOff 方法.

我该如何实现?

请为此提供指导...


The method LogOff is called only if i close tab but not on direct browser close.

How could i achieve this ?

Please guide me for this...

推荐答案

.ajax({ 类型:"GET", url:"/Account/LogOff"//控制器操作方法的URL }); } } 函数BodyClicked(){ isClose = true; }
.ajax({ type: "GET", url: "/Account/LogOff" // the URL of the controller action method }); } } function BodyClicked() { isClose = true; }


仅当我关闭选项卡但不直接关闭浏览器时才调用 LogOff 方法.

我该如何实现?

为此,请为我提供指导.


The method LogOff is called only if i close tab but not on direct browser close.

How could i achieve this ?

Please guide me for this...


如果ajax调用失败,则需要返回false.成功时为true.同样,让ajax调用不异步可能会很好.请参见下面的示例.此示例可能无法解决您的问题,但如果不能解决问题,它应该可以帮助您更好地进行跟踪.

我遇到了onbeforeunload事件的问题,但并非总是触发.如果我没记错的话,如果用户在页面完全加载之前关闭浏览器,则不会触发.我不认为您可以采取任何措施来解决该问题.

You need to return false if the ajax call is not successful. True when it is successful. Also it would probably be good to have the ajax call not be async. See the example below. This example may not fix your problem but it should help you to track it down better if it doesn''t fix it.

I have had problems with the onbeforeunload event not always firing. If I remember correctly it doesn''t fire if the user closes the browser before the page has fully loaded. I don''t believe there is anything that you can do to fix that problem.

function WindowCloseHanlder() {
     var success = false;
     if (!isClose) {


.ajax({ 类型:" , url:" , 异步:错误, 错误:函数(xhr,ajaxOptions,thrownError){ 成功= false ; } 成功:功能(数据){ 成功= true ; } )}; } 返回成功; }
.ajax({ type: "GET", url: "/Account/LogOff", async: false, error: function (xhr, ajaxOptions, thrownError) { success = false; } success: function (data) { success = true; } )}; } return success; }


这篇关于浏览器关闭时的调用控制器方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆