0

以下代码在 IE 中提醒鼠标位置,但在 Firefox 和其他浏览器中,它会提醒“未定义”。

<body onbeforeunload="test(event);">

function test(e){
     if (!e) var e = window.event;
     alert(e.clientX);
}

上面的代码是在浏览器窗口关闭时获取鼠标位置。请告知我需要如何修改上面的代码以返回所有浏览器中的鼠标位置

我的要求是仅在关闭浏览器而不是在页面刷新时打开一个新窗口。有没有其他方法可以在所有浏览器中检测到浏览器关闭?

4

1 回答 1

0

只需添加将鼠标位置存储在变量中的 mousemove 处理程序,如下所示:

<body onbeforeunload="test(event);" onmousemove="storeMouse(event);">

var mouse;
function storeMouse(e)
{
    if(!e) e = window.event;
    mouse = {clientX: e.clientX, clientX:e.clientY};
}


function test(e){
     alert(mouse.clientX);
}
于 2010-08-30T10:16:35.760 回答