如何区分浏览器关闭事件&刷新事件? [英] How to differentiate browser close event & refresh event ?

查看:87
本文介绍了如何区分浏览器关闭事件&刷新事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要求区分浏览器关闭事件和浏览器刷新(使用图标&非F5键)事件。
有人可以给我一个谈论这个的文件吗?

I've a requirement to distinguish between browser close events & browser refresh( using icon & not F5 key) event.
Can someone point me a document which talks about this ?

推荐答案

你不能在IE6中这样做。有一些解决方法,比如在触发卸载事件时检测鼠标位置,但没有一个足够可靠:(

我在网上找到以下代码片段,检查它是否适合你。


You cant do that in IE6. There are some workarounds like detecting the mouse position when the unload event is fired, but there are none which are reliable enough :(

I found the following code snippet on the net check if it works for you.

<html>
<head>
	<title>Detecting browser close in IE</title>


	<script type="text/javascript">

		var myclose = false;

		function ConfirmClose()
		{
			if (event.clientY < 0)
			{
				event.returnValue = 'Any message you want';

				setTimeout('myclose=false',100);
				myclose=true;
			}
		}

		function HandleOnClose()
		{
			if (myclose==true) alert("Window is closed");
		}

	</script>
</head>


<body onbeforeunload="ConfirmClose()" onunload="HandleOnClose()">

<h4>Close browser!</h4>

</body>
</html>







这篇关于如何区分浏览器关闭事件&amp;刷新事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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