0

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?

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Eifel
  • 1
  • 1
  • 1

1 Answers1

0

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);
}
Alex Reitbort
  • 13,504
  • 1
  • 40
  • 61
  • Thanks Alex,the above code returns the mouse cordinates.But i cant generalise a rule for this.One time when I close the browser,clientX & clientY alerts(0,345).The next time it alerts (372,45).The coorinates next to the close(X)button of the browser is required to generalise something like the below code to work in FF : if((window.event.clientX <0 || window.event.clientY <0) {window.open("...")} Since I am getting extremely different ranges of coordinates each time the mouse points to close button, I cannot write a condition similar to the above. Please can you shed some light on this? – Eifel Aug 30 '10 at 11:47