如何通过使用javascript或asp.net关闭浏览器时找到事件 [英] how to find the event when close the browser either by using javascript or asp.net

查看:89
本文介绍了如何通过使用javascript或asp.net关闭浏览器时找到事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在内联代码中



In the inline code

<script language="javascript" type="text/javascript">
window.onbeforeunload = WindowCloseHanlder;
    function WindowCloseHanlder()
        {
           alert("ur closing broswer");
         
            PageMethods.AbandonSession();
        }
</script> 

 <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">

In the code behind(class file)

 [WebMethod]
        public static void AbandonSession()
        {
            HttpContext.Current.Session.Abandon();
            HttpContext.Current.Application.RemoveAll();
        }





但它在IE浏览器中的工作不在Mozilla和chrome



But its work in IE Browser not in Mozilla and chrome

推荐答案

这是一个适用于Chrome& Mozilla。



Here is a code that works in Chrome & Mozilla.

window.onbeforeunload = function(e){
    var msg = 'Are you sure you want to close?';
    e = e || window.event;

    if(e)
        e.returnValue = msg;

    return msg;
}





希望这会有所帮助!

干杯



Hope this helps!
Cheers


这篇关于如何通过使用javascript或asp.net关闭浏览器时找到事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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