<script type="text/javascript"> function UnLoadWindow() { return 'We strongly recommends NOT closing this window yet.' } window.onbeforeunload = UnLoadWindow; </script>orIf you also want to detect whether the user click the X on the top-right of the browser:function handleWindowClose() { if((window.event.clientX<0) || (window.event.clientY<0)) { event.returnValue = "If you have made any changes to the fields without clicking the Save button, your changes will be lost."; } }But I am also afraid you never catch the browser close event for the full 100%. For example, what if the user kills the browser process. Then the onUnload, or even the onBeforeUnload isn't called.
window.onbeforeunload = function (){return ""}; so easy
这样子只有在关闭时才提示:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE> New Document </TITLE> <META NAME="Generator" CONTENT="EditPlus"> <META NAME="Author" CONTENT=""> <META NAME="Keywords" CONTENT=""> <META NAME="Description" CONTENT=""> <script type="text/javascript"> function handleWindowClose() { if((window.event.clientX <0) || (window.event.clientY <0)) { event.returnValue = "If you have made any changes to the fields without clicking the Save button, your changes will be lost."; } } window.onbeforeunload = handleWindowClose; </script> </HEAD> <BODY>
var g_blnCheckUnload = true;
function RunOnBeforeUnload()
{
if(g_blnCheckUnload)
{
window.event.returnValue = 'You will lose any unsaved content';
}
}
function bypassCheck()
{
g_blnCheckUnload = false;
}
onbeforeunload=RunOnBeforeUnload;
</script>
</head>
<body onbeforeunload="RunOnBeforeUnload()">
{
if (!window.confirm("Are you sure to delete the item? ")){
return;
}
}
{
return confirm('确认关闭?');
}
2楼 头像也不赖, 和你本人应该差不多吧 ,呵呵~
just a joke! Do not be angry
{
if (!window.confirm("确定关闭页面?")){
//do something
}
else
{
//do something}
}
是的,IE6上是不会关闭的;
但程序还是不太懂 ,bypassCheck 方法貌似都没有被调用的;
还有 onbeforeunload=RunOnBeforeUnload; 和 <body onbeforeunload="RunOnBeforeUnload()">
都没有重复的必要哦。
问题是:我提出的2个问题还是没解决,呵呵~
继续期待中
嗯, 20-25楼 基本上都想到一块去了。 看来是个不错的主意。
想问下:如何监听窗口的关闭事件呢? 要自己模拟弹出窗口,那么用 onbeforeunload 这个事件就行不通了。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head><body>
<object classid="clsid:B338F4C7-3375-4FA1-BF22-76503A38EB83" id="TT" onclick="alert(1);">
</object>
</body>
</html>
<SCRIPT language=JavaScript>
<!--
var tt = document.getElementById('TT');window.onbeforeunload = function()
{
return false;
}window.onunload = function()
{
tt.onclick();
}
//-->
</SCRIPT>
return 'We strongly recommends NOT closing this window yet.'
} window.onbeforeunload = UnLoadWindow;
</script>orIf you also want to detect whether the user click the X on the top-right of the browser:function handleWindowClose()
{
if((window.event.clientX<0) || (window.event.clientY<0))
{
event.returnValue = "If you have made any changes to the fields without clicking the Save button, your changes will be lost.";
}
}But I am also afraid you never catch the browser close event for the full 100%. For example, what if the user kills the browser process. Then the onUnload, or even the onBeforeUnload isn't called.
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<script type="text/javascript"> function handleWindowClose()
{
if((window.event.clientX <0) || (window.event.clientY <0))
{
event.returnValue = "If you have made any changes to the fields without clicking the Save button, your changes will be lost.";
}
}
window.onbeforeunload = handleWindowClose;
</script>
</HEAD> <BODY>
</BODY>
</HTML>
{
if((window.event.clientX <0) || (window.event.clientY <0))
{
event.returnValue = "";
}
} window.onbeforeunload =frmClose;
呵呵~ 看了你在28楼的代码。可能是我没说太清楚吧, 我的意思是 在调用 onunload 事件的时候,窗口已经是关闭的,此时页面上的一些组件(如 .cab)都已经被卸载。 那么一些操作将不会被执行; 而不是你所写的简单的 onclick 方法。
顺便指出下, 你的那个 onbeforeunload 返回 false 是没有意义的, 只能返回字符串
再次说明我的用意:
我希望能 在调用 onbeforeunload 之后, 选择 确认关闭之后, 而又在调用 onunload 之前, 做些事情。
原因如下:
1. 不能在 onbeforeunload 中处理, 因为无法知道 用户 是不是确认关闭。
2. 不能在 onunload 中处理, 因为在执行该方法时,窗口已经关闭,窗口所加载的一些组件都已经不存在。
所以 , 还望各位高人 指点指点
什么问题都解决了
window.onbeforeunload = function() //author: meizz
{
alert("退出系统!");
window.location.href="loginOut.aspx";
TT方法(1000);
}
loginouu.cs ClientScript.RegisterStartupScript(ClientScript.GetType(), "aa", "<script>window.opener=null;window.close();</script>");
不太懂。。 在 onbeforeunload 事件中 alert(); 不会起到任何作用的啊!试过了,弹出对话框之后会关闭窗口, 并不会跳转的
defalut.aspx <script>window.onbeforeunload = function() //author: meizz
{
var n = window.event.screenX - window.screenLeft;
var b = n > document.documentElement.scrollWidth-20;
if(b && window.event.clientY < 0 || window.event.altKey)
{
//alert("退出系统!");
window.location.href="loginOut.aspx";
sleep(1000);
}
} function sleep(milisecond) { var currentDate,beginDate=new Date(); var beginHour,beginMinute,beginSecond,beginMs; var hourGaps,minuteGaps,secondGaps,msGaps,gaps; beginHour=beginDate.getHours(); beginMinute=beginDate.getMinutes(); beginSecond=beginDate.getSeconds(); beginMs=beginDate.getMilliseconds(); do { currentDate=new Date(); hourGaps=currentDate.getHours() - beginHour; minuteGaps=currentDate.getMinutes() - beginMinute; secondGaps=currentDate.getSeconds() - beginSecond; msGaps=currentDate.getMilliseconds() - beginMs; if(hourGaps<0) hourGaps+=24; gaps=hourGaps*3600+ minuteGaps*60+ secondGaps; gaps=gaps*1000+msGaps; }while(gaps<milisecond); } </script>loginout.cs
if (!IsPostBack)
{
int linID = Convert.ToInt32(Session["linId"].ToString());
DataSet dsIP = bll_online.getAllOnline("select loginIP,logintime from tab_online where linID=" + linID);
string L2 = dsIP.Tables[0].Rows[0]["loginIP"].ToString();
string L1 = dsIP.Tables[0].Rows[0]["logintime"].ToString();
DateTime dt = DateTime.Now;
Model_online.Leavetime = Convert.ToString(dt);
// Model_online.EmpID = empid;
Model_online.Logintime = L1;
Model_online.LoginIP = L2;
Model_online.Online = false;
Model_online.LinID = linID;
bll_online.updateOnline(Model_online);
Session.Remove("loginEmployerId");
//关闭IE
ClientScript.RegisterStartupScript(ClientScript.GetType(), "aa", "<script>window.opener=null;window.close();</script>");
}
关闭页面时清空session。并记录退出时间
在关闭之前 要给出提示“是否确认关闭?”, 但好像你在 onbeforeunload 中就直接处理了;
如果用户 取消关闭,那样就不行了吧~
{
if(confirm('确定要关闭该页面吗?'))
{
window.location.href="loginOut.aspx";
sleep(1000); }
}
这样可以吗?
呵呵~, 要是取消, 那session 都没了。 还不出问题啦
这个我就不试啦, 直接说我的结论了:
“确定要关闭该页面吗?”这个提示窗口会弹出来, 但无论你是确认,还是取消; 页面都会关闭的。
onbeforeunload 只有 return 一个字符串时,才会起到效果。
都说取消什么不做了 还会执行session清空代码吗
60楼的代码,confirm起不到作用,最终页面都会关闭
{
if(confirm('确定要关闭该页面吗?'))
{
window.location.href="loginOut.aspx";
sleep(1000); }
} 重定义的这个方法无论confirm结果返回如何,它都要执行onunload事件,所以页面最终都是要关闭的
这个 就是我郁闷的原因啦;
在执行 onbeforeunload 之后,如果能够有个过渡的东西, 然后再执行 onunload 就好了;
onbeforeunload 只能给出提示, 不能在这个方法中处理。
在 onunload 中处理也不行, 没辙了哎~~~