请教,js如何在关闭浏览器时触发一个事件

请教,js如何在关闭浏览器时触发一个事件,第1张

js监听关闭浏览器事件

Onunload与Onbeforeunload

Onunload,onbeforeunload都是在刷新或关闭时调用,可以在<script>脚本中通过windowonunload来指定或者在<body>里指定。区别在于onbeforeunload在onunload之前执行,它还可以阻止onunload的执行。

2个办法:

一个是客户端主动发送消息告诉服务器浏览器被关了。方法:在window_unonload事件写代码“告诉”服务器。缺点:当浏览器刷新的时候该事件也会被执行!

二、服务器主动“探测”客户端有没有断线,方法:if ResponseIsClientConnected then就知道客户端断线了。缺点:要不断检查客户端,耗资源。

三、用其他一些折中办法

给你一个例子看一下!

<html>

<head>

<title>用户管理</title>

<style>

ts {font-size: 9pt }

rs {background : FFFFEF }

rs1 {background: DEE8C8; color:0050b0}

th1 {background: b7e0ff; color:0050b0}

tr1 {background:d4ebff; color:#0050b0}

tr2 {background:f0f8ff}

fs {font-size: 9pt; color:#0050b0}

btn { background-color: #F3EAE9; font-size: 9pt; height: 19px; width: 40px; border-style: solid; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; cursor: hand}

edit {

BORDER-RIGHT: #4a3163 1px solid; BORDER-TOP: #4a3163 1px solid; FONT-SIZE: 9pt; BORDER-LEFT: #4a3163 1px solid; BORDER-BOTTOM: #4a3163 1px solid; color: #003399; background-color:#F0F8FF}

a:link {COLOR: blue; FONT-FAMILY:宋体; FONT-SIZE: 9pt; TEXT-DECORATION: none}

a:visited {COLOR: blue; FONT-FAMILY: 宋体; FONT-SIZE: 9pt; TEXT-DECORATION: none}

a:hover {COLOR: red; FONT-FAMILY: 宋体; FONT-SIZE: 9pt; FONT-STYLE: normal; TEXT-DECORATION: none}

</style>

<script language="javascript">

var AddUserWin, ModifyNameWin, ModifyRightWin

var ltmAddUser, ltmModifyName, ltmModifyRight

function OnLoad() //进入该页面时最大化

{

windowmoveTo(-4,-4);

windowresizeTo(windowscreenwidth+8,windowscreenheight-20);

return true

}

function OpenAddUser(url)

{

AddUserWin=windowopen(url,"","left=200,top=130,height=280,width=400,resizable=no,scrollbars=no,status=no,toolbar=no,menubar=no,location=no")

CheckAddUserWinClosed()

}

function OpenModifyName(url)

{

ModifyNameWin=windowopen(url,"","left=230,top=150,height=170,width=300,resizable=no,scrollbars=no,status=no,toolbar=no,menubar=no,location=no")

CheckModifyNameWinClosed()

}

function OpenModifyRight(url)

{

ModifyRightWin=windowopen(url,"","left=200,top=150,height=200,width=400,resizable=no,scrollbars=no,status=no,toolbar=no,menubar=no,location=no")

CheckModifyRightWinClosed()

}

//检查新增用户窗口是否关闭

function CheckAddUserWinClosed()

{

if (AddUserWinclosed)

{

clearTimeout(ltmAddUser)

documentlocationhref="userlistasp"

}

ltmAddUser=setTimeout("CheckAddUserWinClosed()", 2000) //2秒检查一次

}

//检查修改用户名和密码的窗口是否关闭

function CheckModifyNameWinClosed()

{

if (ModifyNameWinclosed)

{

clearTimeout(ltmModifyName)

documentlocationhref="userlistasp"

}

ltmModifyName=setTimeout("CheckModifyNameWinClosed()", 2000) //2秒检查一次

}

//检查修改用户权限的窗口是否关闭

function CheckModifyRightWinClosed()

{

if (ModifyRightWinclosed)

{

clearTimeout(ltmModifyRight)

documentlocationhref="userlistasp"

}

ltmModifyRight=setTimeout("CheckModifyRightWinClosed()", 2000) //2秒检查一次

}

</script>

</head>

<body bgcolor="#FFFFFF" leftmargin=0 topmargin=0 class=ts OnLoad="return OnLoad()">

<table width="779" border="0" cellspacing="0" cellpadding="0" height="69">

<tr>

<td colspan="3" background="/image/wz-yhglJPG" height="80"></td>

</tr>

</table>

<br>

<center>

<table bgcolor=blue border=0 cellspacing=1 cellpadding=4 width=85% class=ts>

<th class=th1 colspan=4>用户总列表&nbsp&nbsp<a href=javascript:OpenAddUser("createuserasp")>增加用户</a></th>

<tr class=tr1><td align=center>用户名</td><td align=center>用户所管系统</td><td align=center>修改</td><td align=center>删除</td></tr>

<tr class=tr2><td align=center><a href=javascript:OpenModifyName("modifyuseraspname=11")>11</a></td><td align=center><a href=gqyhgl/indexaspname=11>防汛工情系统</a>   </td><td align=center><a href=javascript:OpenModifyRight("modifywarrantaspname=11")>修改</a></td><td align=center><a href=deleteuseraspname=11>删除</a></td></tr><tr class=tr2><td align=center><a href=javascript:OpenModifyName("modifyuseraspname=112")>112</a></td><td align=center><a href=gqyhgl/indexaspname=112>防汛工情系统</a>   <a href=wzyhgl/indexaspname=112>物资调度系统</a>   </td><td align=center><a href=javascript:OpenModifyRight("modifywarrantaspname=112")>修改</a></td><td align=center><a href=deleteuseraspname=112>删除</a></td></tr><tr class=tr2><td align=center><a href=javascript:OpenModifyName("modifyuseraspname=asdf")>asdf</a></td><td align=center><a href=gqyhgl/indexaspname=asdf>防汛工情系统</a>   <a href=wzyhgl/indexaspname=asdf>物资调度系统</a>   <a href=mediayhgl/indexaspname=asdf>多媒体系统</a>   </td><td align=center><a href=javascript:OpenModifyRight("modifywarrantaspname=asdf")>修改</a></td><td align=center><a href=deleteuseraspname=asdf>删除</a></td></tr><tr class=tr2><td align=center><a href=javascript:OpenModifyName("modifyuseraspname=stg2")>stg2</a></td><td align=center><a href=wzyhgl/indexaspname=stg2>物资调度系统</a>   </td><td align=center><a href=javascript:OpenModifyRight("modifywarrantaspname=stg2")>修改</a></td><td align=center><a href=deleteuseraspname=stg2>删除</a></td></tr><tr class=tr2><td align=center><a href=javascript:OpenModifyName("modifyuseraspname=strong1")>strong1</a></td><td align=center><a href=gqyhgl/indexaspname=strong1>防汛工情系统</a>   </td><td align=center><a href=javascript:OpenModifyRight("modifywarrantaspname=strong1")>修改</a></td><td align=center><a href=deleteuseraspname=strong1>删除</a></td></tr><tr class=tr2><td align=center><a href=javascript:OpenModifyName("modifyuseraspname=wer")>wer</a></td><td align=center><a href=wzyhgl/indexaspname=wer>物资调度系统</a>   </td><td align=center><a href=javascript:OpenModifyRight("modifywarrantaspname=wer")>修改</a></td><td align=center><a href=deleteuseraspname=wer>删除</a></td></tr><tr class=tr2><td align=center><a href=javascript:OpenModifyName("modifyuseraspname=游少爷")>游少爷</a></td><td align=center><a href=mediayhgl/indexaspname=游少爷>多媒体系统</a>   </td><td align=center><a href=javascript:OpenModifyRight("modifywarrantaspname=游少爷")>修改</a></td><td align=center><a href=deleteuseraspname=游少爷>删除</a></td></tr><tr class=tr1><td colspan=4>共有7个用户,分1页<br>[<font color=red>1</font>]</td></tr>

</table>

</center>

</body>

</html>

以上就是关于请教,js如何在关闭浏览器时触发一个事件全部的内容,包括:请教,js如何在关闭浏览器时触发一个事件、asp中如何判断用户已经关闭浏览器、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/9293429.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-26
下一篇 2023-04-26

发表评论

登录后才能评论

评论列表(0条)

保存