var currentKeyCode = -1; function document.onkeydown() { // 本窗口的所有下属页面都必须含有本函数 top.currentKeyCode = event.keyCode;
}
function window.onbeforeunload() { var sw = 0, s0 = "";
if (currentKeyCode == 116)
{
s0 += "刷新窗口!(F5)";
}
else
{
if ((event.altKey)&&(currentKeyCode == 115))
{
s0 += "关闭窗口!(alt+F4)"; sw = 1;
}
else
{
if ((event.clientX > 0)&&(event.clientX < document.body.clientWidth))
{
s0 += "刷新窗口!";
}
else
{
s0 += "关闭窗口!"; sw = 1;
}
}
}
if (sw == 1)
{
event.returnValue = "";
}
else
{
currentKeyCode = -1;
}
}
这是我从网上找的一段JS代码,用浏览器右上角的关闭按钮时好用,但在选项卡上关闭和在任务栏上关闭,这个方法就不作用了,我用的是IE8
}
function window.onbeforeunload() { var sw = 0, s0 = "";
if (currentKeyCode == 116)
{
s0 += "刷新窗口!(F5)";
}
else
{
if ((event.altKey)&&(currentKeyCode == 115))
{
s0 += "关闭窗口!(alt+F4)"; sw = 1;
}
else
{
if ((event.clientX > 0)&&(event.clientX < document.body.clientWidth))
{
s0 += "刷新窗口!";
}
else
{
s0 += "关闭窗口!"; sw = 1;
}
}
}
if (sw == 1)
{
event.returnValue = "";
}
else
{
currentKeyCode = -1;
}
}
这是我从网上找的一段JS代码,用浏览器右上角的关闭按钮时好用,但在选项卡上关闭和在任务栏上关闭,这个方法就不作用了,我用的是IE8
解决方案 »
- 文件读写
- C# C/S 如何获取访问某一页面的用户IP
- 一个简单的正则 20分送上。
- OE的这种Panel中的布局的显示如何做?
- 多线程IP端口扫描器的实现问题
- VS2005里生成类关系图,如何把类之间的关系那个箭头线显示出来
- 说main()方法是整个程序的入口,包括web 应用程序吗?如果包括,在哪里呢
- 在线等待:如何将DataSet--->Excel以及DataSet-->Print 谢谢!
- C# excel编程
- datalist和datagrid有什么区别?再什么什么时候需要用哪个那?
- 如何访问项目文件夹中的文件?
- Word.Application myWord = new Word.ApplicationClass();
web里面也同样可以用这个方法吗?
页面加载时只执行onload
页面关闭时只执行onunload
页面刷新时先执行onbeforeunload,然后onunload,最后onload。这样我们可以在onbeforeunload中加一个标记,在onunload中判断该标记,即可达到判断页面是否真的关闭了。1、Js代码
1.window.onbeforeunload = function() {
2.
3.var n = window.event.screenX - window.screenLeft;
4.
5.var b = n > document.documentElement.scrollWidth-20;
6.
7.
8.if(b && window.event.clientY < 0 || window.event.altKey) {
9.
10.alert("是关闭而非刷新");
11.
12.window.open(this.location);
13.
14.
15.//return false;
16.
17.//window.event.returnValue = ""; }
18.
19.} else{
20.
21.alert("是刷新而非关闭");
22.
23.}
24.
25.}
window.onbeforeunload = function() { 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.open(this.location);
//return false;//window.event.returnValue = ""; } } else{alert("是刷新而非关闭"); }}
2、Js代码
1.function promptOnClose(e){
2.
3.//e = e ? e : windowevent;
4.
5.var promptString = '你是否要离开此页面,离开该页面的信息将不被保存!';
6.
7.//event.returnValue = promptString;
8.
9.return promptString;
10.
11.}
12.
13.if (window != top){
14.
15.top.location.href = "www.baidu.com";
16.
17.}else{
18.
19.if (window.Event) {
20.
21. window.onbeforeunload = function(event) {
22.
23. return promptOnClose(event);
24.
25. }
26.
27.} else {
28.
29. window.onbeforeunload = function() {
30.
31. return promptOnClose(event);
32.
33. }
34.
35. }
36.
37.}
function CloseOpen(event) {
if(event.clientX<=0 || event.clientY<0) {
//获取当前时间
var date=new Date();
//将date设置为过去的时间
alert("关闭网页");
date.setTime(date.getTime()-10000);
//将userId这个cookie删除
document.cookie="zhuangtao;expire="+date.toUTCString();
document.cookie="quanxianzifucuan;expire="+date.toUTCString();
document.cookie="quanxian;expire="+date.toUTCString();
s0 += "关闭窗口!"; sw = 1;
onbeforeunload();
// window.event.returnValue = '关闭浏览器将退出系统.';
}
else
{
alert("刷新或离开");
}
}
var currentKeyCode = -1; function document.onkeydown() { // 本窗口的所有下属页面都必须含有本函数 top.currentKeyCode = event.keyCode;
} function onbeforeunload(){
var sw = 0, s0 = "";
if (currentKeyCode == 116)
{
s0 += "刷新窗口!(F5)";
}
else
{
if ((event.altKey)&&(currentKeyCode == 115))
{
s0 += "关闭窗口!(alt+F4)"; sw = 1;
//获取当前时间
var date=new Date();
//将date设置为过去的时间
alert("关闭窗口");
date.setTime(date.getTime()-10000);
//将userId这个cookie删除
document.cookie="zhuangtao;expire="+date.toUTCString();
document.cookie="quanxianzifucuan;expire="+date.toUTCString();
document.cookie="quanxian;expire="+date.toUTCString();
}
else
{
if ((event.clientX > 0)&&(event.clientX < document.body.clientWidth))
{
s0 += "刷新窗口!";
}
else
{
//获取当前时间
var date=new Date();
//将date设置为过去的时间
alert("关闭网页");
date.setTime(date.getTime()-10000);
//将userId这个cookie删除
document.cookie="zhuangtao;expire="+date.toUTCString();
document.cookie="quanxianzifucuan;expire="+date.toUTCString();
document.cookie="quanxian;expire="+date.toUTCString();
s0 += "关闭窗口!"; sw = 1;
}
}
}
if (sw == 1)
{
event.returnValue = "";
}
else
{
currentKeyCode = -1;
}
}<body onunload="CloseOpen(event)" ></body></html>
我最后用的是这个,但还是没法判断在任务栏上直接关闭浏览器,结帖
if(event.clientX<=0 || event.clientY<0) {
//获取当前时间
var date=new Date();
//将date设置为过去的时间
alert("关闭网页");
date.setTime(date.getTime()-10000);
//将userId这个cookie删除
document.cookie="zhuangtao;expire="+date.toUTCString();
document.cookie="quanxianzifucuan;expire="+date.toUTCString();
document.cookie="quanxian;expire="+date.toUTCString();
s0 += "关闭窗口!"; sw = 1;
onbeforeunload();
// window.event.returnValue = '关闭浏览器将退出系统.';
}
else
{
alert("刷新或离开");
}
}
var currentKeyCode = -1; function document.onkeydown() { // 本窗口的所有下属页面都必须含有本函数 top.currentKeyCode = event.keyCode;
} function onbeforeunload(){
var sw = 0, s0 = "";
if (currentKeyCode == 116)
{
s0 += "刷新窗口!(F5)";
}
else
{
if ((event.altKey)&&(currentKeyCode == 115))
{
s0 += "关闭窗口!(alt+F4)"; sw = 1;
//获取当前时间
var date=new Date();
//将date设置为过去的时间
alert("关闭窗口");
date.setTime(date.getTime()-10000);
//将userId这个cookie删除
document.cookie="zhuangtao;expire="+date.toUTCString();
document.cookie="quanxianzifucuan;expire="+date.toUTCString();
document.cookie="quanxian;expire="+date.toUTCString();
}
else
{
if ((event.clientX > 0)&&(event.clientX < document.body.clientWidth))
{
s0 += "刷新窗口!";
}
else
{
//获取当前时间
var date=new Date();
//将date设置为过去的时间
alert("关闭网页");
date.setTime(date.getTime()-10000);
//将userId这个cookie删除
document.cookie="zhuangtao;expire="+date.toUTCString();
document.cookie="quanxianzifucuan;expire="+date.toUTCString();
document.cookie="quanxian;expire="+date.toUTCString();
s0 += "关闭窗口!"; sw = 1;
}
}
}
if (sw == 1)
{
event.returnValue = "";
}
else
{
currentKeyCode = -1;
}
}
代码如下,
<html>
<head><script language="JavaScript"> <!--
function checkRefresh()
{
if( document.refreshForm.visited.value == "" )
{
// 非刷新,改变默认值
document.refreshForm.visited.value = "1"; // 非刷新情况下的其它应用逻辑
}
else
{
// 刷新页面导致下面代码的执行 // 刷新情况下的其它应用逻辑
}
} -->
</script></head><body onLoad="JavaScript:checkRefresh();"><form name="refreshForm">
<input type="hidden" name="visited" value="" />
</form></body>
</html>我没对代码进行测试,只是路过。。