views:

19

answers:

2

Hi all, so the onbeforeunload event doesn't have mouse information, so how do I get mouse XY? I am trying to know if the user clicked the close buttom on the browser to log them out right away (20minutes of session is too long for resource, but, we will not shorten it to make customer angry).

On IE, I can simply access mouse info to figure out if the user clicked close buttom or not during the onbeforeunload. For FF, I am suppose to use the parameterized event, which sadly has no mouse info at all.

(Note, this is not needed if there is a REAL onBrowserClose event. Since this is not the case, I am working on the alternatives.)

Thank you all.

Just to make sure people understand it, here is example.

<head runat="server">
    <title>Untitled Page</title>
<script type="text/javascript">
    function test(event) {
        var X = window.event ? window.event.screenX : event.clientX;

        // IE only because event.clientX for FF = fail.
        if (X == null) {
            alert("No X");
            return;
        }
    };
</script>

</head>
<body onbeforeunload="test(event);">
A: 

You could populate the mouse movement into variables on the mousemove event, and then use those variables in the onbeforeunload event...

<head runat="server">
    <title>Untitled Page</title>
<script type="text/javascript">
    var X = 0;

    function MouseMoveHandler(event) {
        X = window.event ? window.event.screenX : event.clientX;

        // IE only because event.clientX for FF = fail.
        if (X == null) {
            alert("No X");
            return;
        }
    };

    function BeforeUnloadHandler() {
        alert(X);
    }
</script>

</head>
<body onbeforeunload="BeforeUnloadHandler();" onmousemove="MouseMoveHandler(event);">
Sohnee
A: 

Sorry, created new account because I don't know how to log back in.

Thanks for the answer, but, I really need mouse event "outside" of the body/document to get the mouse position when clicking browser close button. So far, your solution will only capture the mouse within boy/document, which is not enough.

Thanks for the help.