The following piece of code alerts the mouse position in IE , but in Firefox and other browsers, it alerts "undefined".
<body onbeforeunload="test(event);">
function test(e){
if (!e) var e = window.event;
alert(e.clientX);
}
The above code is to get the mouse position when the browser window is closed.Please advise how I need to amend the above code to return the mouse position in all browsers
My requirement is to open a new window only when a browser is closed and NOT on page refresh. Is there any other way by which the browser close can be detected in all browsers?
just add mousemove handler that will store mouse position in variable, like this:
<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);
}
精彩评论