日期:2014-05-16  浏览次数:20300 次

js监听关闭浏览器

<script type="text/javascript">

window.onbeforeunload = function() //author: meizz?

{?

? ? window.location="http://localhost:8080/tt/servlet/tt?action=loginOut";

alert("关闭窗口之前");?

??

}

window.onunload = function() //author: meizz?

{?

alert("关闭窗口");?

??

}

</script>

?

?

String action = request.getParameter("action");

? ?HttpSession session = request.getSession();

if("login".equals(action))

{

? ? if(null == session.getAttribute("flag"))

? ? {

? ? ? ? session.setAttribute("flag", 1); ?

? ? }

? ? System.out.println("login \t:"+ ?session.getAttribute("flag"));

? ??

}

else if("loginOut".equals(action))

{

session.removeAttribute("flag");

System.out.println("loginOut \t:"+ ?session.getAttribute("flag"));

}

}