在使用window.onbeforeunload时捕获用户响应 [英] Capture user response when using window.onbeforeunload

查看:221
本文介绍了在使用window.onbeforeunload时捕获用户响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法捕捉用户点击OK或CANCEL?



如果用户离开页面,我只需要做一些事情。

解决方案

这是我正在使用的解决方案,只有当用户从页面导航时,我才需要做某些事情(如清除会话)。



我有2个全局变量

  var clearSession = true; 
var confirmExit = true;


window.onbeforeunload = function(){return confirmExit(); }
window.onunload = function(){return clearSession(); }

函数confirmExit(){
if(needToConfirm == true){

返回退出页面;
}
}


函数clearSession(){

if(clearSession == true){
alert(杀死服务器上的会话!!!);
PageMethods.ClearSession();
}
}

然后当然,在每一页上提交/按钮/下拉列表等...您需要确保上述全局变量设置为false。



希望这有助于某人。


Is there a way to capture whether the user clicked OK or CANCEL?

I need to do something ONLY if the user is leaving the page....

解决方案

Here is the solution I am using in a situation where I need to do something (such as clear the session) ONLY when the user navigates from the page.

I have 2 global vars

var clearSession = true;
var confirmExit = true;


    window.onbeforeunload = function() { return confirmExit(); }
    window.onunload = function() { return clearSession(); }

function confirmExit() {
    if (needToConfirm == true) {       

        return "exit page?";
    }
}


function clearSession() {

     if (clearSession == true) {
        alert("Killing the session on the server!!!");
        PageMethods.ClearSession();
    }
}

Then of course, on every page submit/button/drop down list etc...you need to make sure that the above global variables are set to false.

Hope this helps someone.

这篇关于在使用window.onbeforeunload时捕获用户响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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