views:

40

answers:

2

Hi,

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?

Thanks in advance.

A: 

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
Eifel
A: 

Thanks Alex, the above code returns the mouse cordinates. But i can't generalise a rule for detecting the browser close with the mouse coordinates.i.e.one time when I close the browser, clientX & clientY alerts someting like(0,345)..The next time when I close the same browser it alerts (372,45)..I would like to get the coorinates next to the "close(X)" button of the browser so that I can generalise something like the below code to work in FireFox (as the below works only in IE):

 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