开发者

Issue with onbeforeunload event handling in firefox [duplicate]

开发者 https://www.devze.com 2023-01-14 07:14 出处:网络
This question already has answers here: 开发者_Python百科Crossbrowser onbeforeunload? (5 answers) Closed 8 years ago.
This question already has answers here: 开发者_Python百科 Crossbrowser onbeforeunload? (5 answers) Closed 8 years ago.

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);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号