我通过此window.onbeforeunload=HandleOnClose;
这在IE中运行良好,但在其他浏览器中没有。此代码特定于IE,但这在其他浏览器中不起作用。我们如何在所有浏览器中检测浏览器的关闭事件?
function ConfirmClose()
{
if (isIE7Min) //To check for IE Version 7.0 and above
{
var n = window.event.screenX - window.screenLeft;
var b = n > document.documentElement.scrollWidth-20;
if ((b && window.event.clientY < 0) || window.event.altKey)
{
if (!self.closed)
{
refresh = true;
}
}
}
else //IE Version 6.0 and below
{
if (event.clientY < 0 && event.clientX < 0)
{
if (!self.closed)
{
refresh = true;
}
}
}
}
/*Function for onunload*/
function HandleOnClose()
{
ConfirmClose();
if (isIE7Min)
{
if ((window.event.clientX > 0 && window.event.clientY < 0) || window.event.altKey)
{
if (refresh == true)
{
window.open("/DealPricingJavaUAT/DealPricingJsp/Home/logout.jsf");
//Code That redirects to the Session Invalidation Page
}
}
}
else
{
if (event.clientY < 0 && event.clientX < 0 || window.event.altKey)
{
if (refresh == false)
{
window.open("/DealPricingJavaUAT/DealPricingJsp/Home/logout.jsf");
// Code That redirects to the Session Invalidation Page
}
}
}
}
答案 0 :(得分:1)
我认为不可能始终将用户注销。如果用户的Web浏览器出现崩溃,则永远不会执行客户端代码。有些Web浏览器会非常快地关闭自己,并且不会等待执行客户端代码。
您应该找到一种替代解决方案,例如,如果用户在特定时间段内未处于活动状态,则在服务器端注销。
答案 1 :(得分:0)
无法检测用户何时关闭浏览器。 如果您使用Java并使用cookie来检查用户是否已登录,则可以 做cookie.setMaxAge(-1)。
这将确保在浏览器关闭时删除cookie。