判断浏览器关闭还是刷新:
onclick方法也可以这样写:
<html>
<head>
<script type="text/javascript">
var num=0;
window.onbeforeunload=function(){
if(num==0){
window.location.href="<%=basePath%>login.do";
}
num=0;
};
function cl(event) {
if(event.keyCode==116){
num++;
}
}
</script>
</head>
<body style="text-align: center;height: 100%;" onkeydown="cl(event)">
index
</body>
</html>onclick方法也可以这样写:
window.document.onkeydown = disableRefresh;
function disableRefresh(evt){
alert(evt.keyCode);
};
本文介绍了一种通过JavaScript实现的方法来判断用户是否刷新或关闭了浏览器窗口。利用window.onbeforeunload事件监听器和键盘事件keyCode来区分两种行为,并提供了一个简单的示例代码。
173

被折叠的 条评论
为什么被折叠?



